Some reactions appear to gain or lose mass, but no atoms are lost or made. The change can usually be explained because a reactant or product is a gas whose mass was not measured. This happens in a non-enclosed system, which is a container open to the air.
When a metal reacts with oxygen from the air, for example 2Mg + O2 → 2MgO, the mass of the solid increases. The oxygen particles from the air join the metal atoms, so the oxide contains more atoms than the metal alone and the mass of the oxide is greater than the mass of the metal.
In the thermal decomposition of a metal carbonate, for example CaCO3 → CaO + CO2, carbon dioxide gas is made. The gas particles move freely and spread out, so carbon dioxide escapes into the air. The metal oxide is the only solid product left, so the mass of the solid decreases.
In a sealed container no gas can escape, so the total mass stays constant. The mass of the products is always equal to the mass of the reactants once the gas is taken into account.