For clarity:
This only happens if you enter the edit troops menu from PvP, then back out of the fight completely. The team you had open on the edit troops list will then become your defense team (or the first team, if you collapse the list before exiting). It doesn’t matter whether or not you actually modify a team, and the glitch does not trigger if you use the PvP edit troops button and continue into battle, nor does it trigger using the edit troops button from a non-PvP battle.
With this knowledge, it is 100% avoidable, but still a bit annoying.