Mass is always conserved, but in a non-enclosed system the mass on a balance can appear to change. This happens when a gas is a reactant or a product. A gas can enter or leave an open container, and the balance only measures what is left inside.
When magnesium burns in air, the mass of solid increases. The magnesium combines with oxygen from the air to make magnesium oxide, so the oxide has the mass of the magnesium plus the mass of the oxygen it gained. When calcium carbonate or copper carbonate is heated in an open tube, the mass of solid decreases. The carbonate breaks down to a metal oxide and carbon dioxide, and the carbon dioxide escapes. The same happens when a carbonate reacts with acid in an open flask. For example, 5.0 g of copper carbonate may leave 3.2 g of solid, so 1.8 g of gas escaped.
The particle model explains this. The particles in a gas are far apart and move quickly in all directions, so a gas product can escape into the surroundings. The atoms have not vanished, they are now in the gas particles. If the same reaction is done in a sealed container, the gas is trapped and the total mass stays constant. Once the mass of the gas is counted, the mass of reactants and products is equal.