Sometimes, the same element may exhibit one valency in one compound and another valency in other compound. This property is called as variable valency.
The reason behind Why they lose this extra electron is purely a matter of the specific element, what electron shell they are losing/gaining electrons in, and their relative energies to the previous/ next electron shell. Also their ability to share electrons between these shells