This reaction is a redox reaction, which means both oxidation and reduction happen at the same time.
Oxidation is when a substance gains oxygen. In this reaction, the metal atoms gain oxygen from the acid. For example, magnesium atoms become magnesium oxide (which is part of the salt):
Mg→Mg2++2e− (the metal loses electrons, so it is oxidised)
Reduction is when a substance loses oxygen or gains electrons. The acid provides hydrogen ions, which gain electrons to form hydrogen gas:
2H++2e−→H2 (the hydrogen is reduced)
To remember which is which, use the mnemonic OIL RIG: Oxidation Is Loss of electrons, Reduction Is Gain of electrons. In the metal-acid reaction, the metal loses electrons (oxidation) while the hydrogen ions gain electrons (reduction).