Looking at the electronic configurations of Sodium and Magnesium:
,
we can see that they both have their valence electrons in the 3rd shell.
But because Magnesium has a higher nuclear charge of +12 because of its 12 protons than that of Sodium's +11, the nucleus of Magnesium attracts its outer electrons more strongly than Sodium does. This makes the Magnesium atom smaller.