Apparently on the update notes for 47.05 this (or some sort of variation of it) was listed as fixed by Toady, id definitely check your current version but wouldnt be suprised if it still happens. Perhaps a DFhack script to remove tokens and un-set their current marauding state from them after checking a event for taming (if you can narrow it down) might be a better long term solution if any sort of script is out of there already.
I see it sometimes in reverse, a few fringe DFhack scripts have that have a ability to summon creatures and backfire into a announced maurader monster even without specialist token additions, so perhaps its a problem that marauding for them goes dormant until confronted by the militiadwarves, but i guess it'd need some science.
The thing toady fixed with 47.05 is something unrelated to what I'm talking about (
https://www.bay12games.com/dwarves/mantisbt/view.php?id=10731)
I tried messing around with with gm-editor to see if there's anything I could do to fix the issue. I checked the marauder/crazed status of the tamed megabeast and found it was set to False anyway (lol).
My gut feeling told me it was something related to the historical figure status of the megabeast and it indeed was the problem:
By setting the important_historical_figure flag in flags1/2 to False, and also setting the historical figure id to -1 (both of them), my militia dwarves finally stopped trying to murder the tame megabeast.
I guess the problem is that the militia dwarves somehow remember that megabeast with historical_figure_id X is an enemy of their civ and that's why they try to kill it. I don't really have any experience with actual dfhack scripting, so if anyone is interested in writing one, just know that it would be something like -> After taming the megabeast, erase its historical figure status.