When the fighting is over, a magical girl must return to being an ordinary civilian, and thus the colorful costume and change of hairstyle must go.
When a magical girl is defeated in battle, they sometimes get forcibly ejected from their transformed states.
Most of the time they simply return to wearing their normal outfits, but once in a while it ends up being different.
From what little I've seen of Sailor Moon, having the transformation forcibly cancelled causes the outfit to explode into a stream of red ribbons, leaving them wearing nothing but that. I only remember seeing this happen in the SuperS movie. It's been a loooong time.
In Heartcatch and Happiness Charge Precure, any Cure who is defeated in battle ends up wearing only the glowy dress from their transformation sequence. There are two strange exceptions, Cure Flower, who was never shown transforming, ends up back in her normal clothes after being defeated in battle.
In the first episode we see Cure Moonlight lose a battle against Dark Precure. Her transformation cancels, but her hair stays transformed. Obviously, this is to conceal her identity, and it's one of those things that wouldn't even occur to me watching the first time.
I started Anyamal Tantei Kiruminzoo last night and one of the odder things about it is that when the characters willingly change back, they appear naked, with their clothes fluttering down from above seconds later. Comedy I guess.
I've always wondered why things like this happen. It seems like it would be less work on the animator's part to just have them change back into their normal clothes. I've learned recently that being a show made for little girls doesn't exclude an anime from having fanservice, so that's always a possible explanation, with Heartcatch in particular having action scenes that seem catered toward an audience other than little girls.