$$Mg(s) + 2HCl(aq) → MgCl_2(aq) + H_2(g)$$
The hydrogen gas is produced as a result of the reaction between the magnesium and the hydrochloric acid. The magnesium atoms lose electrons to the hydrogen atoms, which results in the formation of magnesium ions and hydrogen gas.