Rainbow counts as a :fire: energy when it gets attached to Magmortar, and triggers Flame Body. The attachment of the Rainbow also triggers the damage counter placement. Since both effects are triggered by the same event, the person controlling the Pokemon decides the order they happen.
So, it could remove the damage, or not, as you choose.