The trend can be explained by the stability associated with filled subshells.
The ionization energy refers to the energy required to remove one mole of electrons from one mole of atoms in the gaseous state to form ions. The trend you've mentioned - that the first ionization energy of beryllium (Be) is greater than boron (B), magnesium (Mg) is greater than aluminum (Al), and nitrogen (N) is greater than oxygen (O) - can be explained by understanding the underlying principles of atomic structure.
Atoms with filled subshells have higher ionization energies because removing an electron requires breaking a stable configuration. Additionally, effective nuclear charge and electron shielding also play roles in determining ionization energy trends across periods and groups in the periodic table