or

Ionisation energy of an element depends on several factors including the atomic size, effective nuclear charge, electronic configuration, penetration of electrons and so on.

But in case of Sodium and magnesium, the first ionisation energy of Sodium is higher because as the atomic size of sodium is comparatively smaller than Magnesium so it experiences strong effective nuclear charge hence, more amount of energy is required to release valence electron in its ground state.