Re: It's not complicated
In South America this might well be resolved by a mediator.
They recognise the practise of "diplomatic asylum". So when the military coup happens, various members of the deposed government run for friendly embassies. Then if the coup is put down, they're on hand to pop back out and resume governing. But if it succeeds, they hang around there for a bit, until a deal is done and they go into exile.
Military coups haven't been so widespread as they used to be in South America, but this still happens. The idea is that it's a lot better than being strung up from a lamppost, and as you're government might itself get overthrown, there's an incentive to keep that fall-back position open.
However diplomatic asylum isn't recognised in the Vienna Conventions. Which are the international law on diplomacy. And it's not something the Foreign Office holds with. According to my favourite UK ex-ambassador, Charles Crawford, FCO guidelines are to usher such people out of your embassy as fast as possible to avoid massive embarassment to both you and your host country. An ambassador can't be a discrete source of information and communication with between governments when he's personally involved in a noisy public dispute. Especially one that rudely exploits his embassy's diplomatic status to embarrass his host government - and make political trouble for them.