Magnesium burns in presence of carbon dioxide because, the oxygen in the carbon dioxide is able to bond with magnesium and produce an oxide. Carbon is formed as a biproduct the carbon which is formed is in the form of soot.
Burning or Mg in CO2 forms Magnesium oxides.
2 Mg + CO2 → 2 MgO + C (soot).
Magnesium is a alkaline earth metals, In its pure form, it burns in air to produce oxides.
2 Mg + O2 → 2 MgO
Magnesium is one of the alkaline-earth metals, and is one of the most common elements in the Earth's crust. In its pure form, it is silvery white, and relatively soft. It burns in air with a brilliant white light, and for this reason is often used in flares and fireworks.
2Mg(s) + O2(g) ——> 2MgO(s)
The high temperatures reached during the combustion also allow small amounts of magnesium to react with nitrogen in the air, producing magnesium nitride:
3Mg(s) + N2(g) ——> 2Mg3N2(s)
Magnesium also burns in an environment of carbon dioxide
2Mg(s) + CO2(g) ——> 2MgO(s) + C(s)