Using a first-principles total-energy pseudopotential method, we investigate the transition mechanism for a pressure-induced martensitic transformation hcp→bcc which occurs in Mg at pressures around 50 GPa. Two internal structural degrees of freedom are selected and one lattice is transformed into the other by relaxing these two parameters continuously. One of the parameters characterizes the relative displacement of the hexagonal layers and corresponds to a transverse phonon at the Brillouin-zone edge A in the hexagonal structure. The other characterizes the distortion of the internal hexagonal angles and corresponds to uniform strain along one of the hcp directions. The interaction between these two distortion modes causes important anharmonic effects in the zone-edge phonon and provides a low-energy path for the structural transition. The small activation barrier at the transition indicates that quantum fluctuations between the two structures could be taking place.