Metals like iron are examples of transition metals, which is why they exhibit variable valency. All transition metals show this behavior. This happens when the d-orbitals also lose their valence electrons when forming bonds with other elements or ligands.
Naturally, transition elements use the electrons in their s-orbitals when forming bonds, but they can still use electrons in their incomplete d-orbitals to form bonds with other elements. For instance, iron can lose the two electrons in its valence shell when forming bonds, and it can still use the electrons in its penultimate shell to form bonds.
That is why iron can have oxidation numbers of +2 and +3. This can be attributed to the energy difference between their orbitals. Another reason why some elements show variable valency is because of the difference in their electronic configurations. This is why, in a particular group, you might notice a progressive decrease in the oxidation number.