The electronic configuration of magnesium is:
1s² 2s² 2p⁶ 3s² = [Ne] 3s²
This means that for a magnesium atom, in order to have its outermost orbital full, the easiest way would be to lose the two outermost electrons. This is seen in its relatively low first two ionization energies. The third ionization energy is several times higher because the ion would move from a stable form to an highly unstable form. (Mg⁺² → Mg⁺³ + e⁻).
Sodium only has one electron in its outermost orbital, so its first ionization energy would be several times lower than the second.