Answer
Diagram the reaction:
Work Step by Step
1) The electron configuration of $Mg$ is $$[Ne]3s^2$$
The electron configuration of $O$ is $$1s^22s^22p^4$$
2) We see that $Mg$ has 2 valence electrons and $O$ has 6 valence electrons.
According to octet rule, atoms tend to lose, gain or share electrons until they have 8 valence electrons.
We see that if $Mg$ lose both of its valence electrons, it would reach an octet $$1s^22s^22p^6$$
which is also the electron configuration of $Ne$.
In contrast, if $O$ gains 2 electrons, it would reach an octet $$1s^22s^22p^6$$ with 8 valence electrons in the $n=2$ shell.
Therefore, $Mg$ would much likely transfer its 2 valence electrons to $O$. Losing 2 electrons make a neutral $Mg$ atom change into $Mg^{2+}$, while gaining 2 electrons makes a neutral $O$ atom change into $O^{2-}$. The oppositely charged particles form bond to give the ionic substance $MgO$.
In diagram, the reaction would be