This is a redox reaction where oxidation and reduction take place. Here, Mg(s) oxidizes into Mg²⁺(aq) by increasing oxidation number from 0 to +2 while Ni²⁺(aq) reduces into Ni(s) by decreasing oxidation number from +2 to 0.
Oxidation
Mg(s) → Mg²⁺(aq) + 2 e
Reduction
Ni²⁺(aq) + 2 e → Ni(s)
Hence, 2.0 moles of electrons lost by Mg(s) when 2.0 moles of electrons are gained by Ni²⁺(aq).