We derive a mass formula and a mass variation law for asymptotically flat, stationary spacetimes, invariant under two commuting rotational symmetries, in a general five dimensional theory of gravity coupled to an arbitrary set of Maxwell fields and uncharged scalar fields. If the spacetime is everywhere regular, these mass formulas reduce to a sum of magnetic flux terms defined on its non-trivial 2-cycles. If there is a black hole, we obtain a mass variation law more general than previously obtained, which also has contributions from the 2-cycles exterior to the black hole. This can be interpreted as the first law of black hole mechanics in a background soliton containing bubbles.