Because of following reasons
1- The electronic configuration of Nitrogen is 2s22p3 while that of Oxygen is 2s2 2p4
From this we can see that , nitrogen is having half filled p-orbital , which is highly stable and Nitrogen will not lose an electron as due to this its stability is lost , and hence for taking out an electron will more energy that means it will have higher ionization enthalpy. While in case of Oxygen it will wish to lose an electron in order to attain half filled stable configuration, hence is has a less ionization enthalpy.
2) Electron gain enthalpy is the energy required to add an electron to the isolated gaseous atoms of element, and for Be and Mg it is positive unlike electronegative atoms, which have negative Electron gain enthalpy . This is all because Be and Mg are electropositive elements and addition of an electron will cause an unstability to the atom, hence they will oppose the addition of electron to it , so large amount of energy is required.