Flattening everything then removes the mask, regardless of the stacking order.
Ironically, when you backsave to Illustrator 3 or 4 format, the layers are discarded but every mask remains intact. The downside is that all gradients are converted to blends.