The MUSE Hubble Ultra Deep Field Survey. XII. Mg II emission and absorption in star-forming galaxies