Hello!
I have some insight into this, I believe.
When trading a town or castle you own to an NPC, the militia members are still considered your troop and are then ejected from the castle. (I know this because you can engage with the militia unit, and it brings up a battle screen. During the fight, they carried my banner)
However, something in the game's code still recognize them as the proper unit for that settlement, so if new militia are generated, they are generated inside that unit (Even though it's not inside the castle).
In the case of a mercenary party owning a city and then leaving, because they are irregular troops, the flags within the code likely lead the same route as if the town or castle was traded (or visa-versa)
I'm guessing it would be easiest to have the devs check the militia code in relation to trading a city or town. A missing tag or error within the code is likely there, and it is causing a cascading effect that result in the Militia standing outside. I am nearly positive that the issue is only that the ownership of Militia remains with the original owner of the traded/mercenary own settlement. (Sort of like that bug where the player being in an army means that winning the battle results in a losing screen, and losing the battle means a winning screen)
To replicate, capture a city/castle via combat, then trade it to another faction in exchange for one of their cities/castles