Using two first-principles computer simulation techniques, path integral Monte-Carlo and density functional theory molecular dynamics, we derive the equation of state of magnesium in the regime of warm dense matter, with densities ranging from 0.43 to 86.11 g cm −3 and temperatures from 20,000 K to 5 × 10 8 K. These conditions are relevant for the interiors of giant planets and stars as well as for shock compression measurements and inertial confinement fusion experiments. We study ionization mechanisms and electronic structure of magnesium as a function of density and temperature. We show that the L shell electrons 2s and 2p energy bands merge at high density. This results into a gradual ionization of the L-shell with increasing density and temperature. In this regard, Mg differs from MgO, which is also reflected in the shape of its principal shock Hugoniot curve. For Mg, we predict a single broad pressure-temperature region where the shock compression ratio is approximately 4.9. Mg thus differs from Si and Al plasma that exhibit two wellseparated compression maxima on the Hugoniot curve for L and K shell ionizations. Finally we study multiple shocks and effects of preheat and precompression.