  Using {\it ab initio} molecular dynamics we investigate the electronic and
lattice structure of $A$Fe$_2$As$_2$ ($A$=Ca, Sr, Ba) under pressure. We find
that the structural phase transition (orthorhombic to tetragonal symmetry) is
always accompanied by a magnetic phase transition in all the compounds, while
the nature of the transitions is different for the three systems. Our
calculations explain the origin of the existence of a collapsed tetragonal
phase in CaFe$_2$As$_2$ and its absence in BaFe$_2$As$_2$. We argue that
changes of the Fermi surface nesting features dominate the phase transition
under pressure rather than spin frustration or a Kondo scenario. The
consequences for superconductivity are discussed.
