Answer
$Mg$ atom loses electrons in the reaction.
Work Step by Step
To reach an octet, $Mg$ atom needs to lose 2 valence electrons, while $O$ atom, in constrast, needs to gain 2 electrons.
Therefore, $Mg$ atom is willing to lose 2 valence electrons to $O$ atom so that both can achieve an octet.
That means $Mg$ atom loses electrons in the reaction.