We study M-theory on a Calabi-Yau fourfold with a smooth surface $S$ of
$A_{N-1}$ singularities. The resulting three-dimensional theory has a
$\mathcal{N}=2$ $SU(N)$ gauge theory sector, which we obtain from a twisted
dimensional reduction of a seven-dimensional $\mathcal{N}=1$ $SU(N)$ gauge
theory on the surface $S$. A variant of the Vafa-Witten equations governs the
moduli space of the gauge theory, which, for a trivial $SU(N)$ principal bundle
over $S$, admits a Coulomb and a Higgs branch. In M-theory these two gauge
theory branches arise from a resolution and a deformation to smooth Calabi-Yau
fourfolds, respectively. We find that the deformed Calabi-Yau fourfold
associated to the Higgs branch requires for consistency a non-trivial four-form
background flux in M-theory. The flat directions of the flux-induced
superpotential are in agreement with the gauge theory prediction for the moduli
space of the Higgs branch. We illustrate our findings with explicit examples
that realize the Coulomb and Higgs phase transition in Calabi-Yau fourfolds
embedded in weighted projective spaces. We generalize and enlarge this class of
examples to Calabi-Yau fourfolds embedded in toric varieties with an $A_{N-1}$
singularity in codimension two.