Electronic configuration of Na and Mg are
Na = 1s2 2s2 2p6 3s1
Mg = 1s2 2s2 2p6 3s2
First electron in both cases has to be removed from 3s-orbital but the nuclear charge of Na (+11) is lower than that of Mg (+12) therefore first ionization energy of sodium is lower than that of magnesium.
After the loss of first electron, the electronic configuration of
Na = 1s2 2s2 2p6
Mg = 1s2 2s2 2p6 3s1
Here electron is to be removed from inert (neon) gas configuration which is very stable and hence removal of second electron requires more energy when compared to Mg.
Therefore, second ionization enthalpy of sodium is higher than that of magnesium.