The hard part would be the trigger itself; you'd need an event that triggers whenever a city is captured, and not just the events that trigger only when a player can see it happening. The easiest way to get around this is a start-of-turn event:
Loop over all players.
Loop over all cities owned by that player.
If the city is in resistance/anarchy, generate a militia unit and assign it to the Barbarian faction.
(end loops)
This'd allow for the creation of these units, without the headache of trying to ensure an OnCityCaptured event fires correctly. The biggest downside would be that this'd trigger if you switched from Piety to Rationalism (one turn of anarchy for all cities!). So you'd probably want an N>1 check; a city only spawns militia if it has more than 1 turn of anarchy remaining.