The electron configuration for Mg is
Mg = 1s2 2s2 2p6 3s2
Al = 2s2 2s2 2p6 3s2 2p1
The discontinuity arises because the add electron for Al goes into a different orbital (higher energy in this case). You can draw the same kind of thing and observe the same thing for S vs P where the added electron for S must be paired vs P in which all three electrons in the p orbital are unpaired.