I’ll try my best to make things within the rules
Normally, your best bet for “if X happens, then I want a spell that does Y” is contingency. Unfortunately for your purposes, in 5e, Contingency can only be cast on Self, and can only trigger spells of 5th level or lower (which eliminates using Teleport or Word of Recall). Further, contingency also triggers only spells that have a casting time of one action, which means we can’t use it to try to lower the level of teleport by using a teleportation circle or have it cast and then immediately trigger a glyph of warding with one of the aforementioned spells stored in it.
Without access to Contingency, you’re going to need a fair bit of ‘creative interpretation’ for this, but here goes.
First, you need a really nice Ring of Mind Shielding. In mean, we’re going to have to trick that bling out.
The ring of mind shielding needs to be in the form of a tiny replica of an exquisite treasure chest. Something like this will do. (Note to mods: this is for illustrative purposes only and hopefully will be understood as not an advertisement on my part).
Also, the ring of mind shielding has to serve as a ring of spell storing as well. (Note: I don’t have a stat block for Xanathar but I am assuming it is both a beholder and not a caster).
Now that you have your three-in-one ring, have a caster with access to the Secret Chest spell store one in the ring.
Xanathar takes the ring and wears it – perhaps as a tongue piercing? While doing so, he casts the secret chest spell while touching the larger version of the chest (“an exquisite chest, 3 feet by 2 feet by 2 feet, constructed from rare materials worth at least 5,000 gp, of which the ring is the replica”), thus fitting the conditions for casting the secret chest spell, “You must touch the chest and the miniature replica that serves as a material component for the spell.” Or perhaps not, if using the spell storing function means you don’t need material components. In that case I guess you don’t need the miniature treasure chest on the ring, but it is still a nice touch.
In casting the spell, Xanathar sends the secret chest into the Ethereal (for at least 60 days risk-free).
Pause for party to arrive.
Your party assaults Xanathar and appears to have killed him, at which point the mind shielding function of the ring activates and his soul enters it. Once he is inside the ring, he calls for the Secret Chest…and here is where we hand-wave a bit.
“While the chest remains on the Ethereal Plane, you can use an action and touch the replica to recall the chest.” Can Xanathar’s soul within the ring be considered a “you” that is “touching” the replica? Fortunately, there is no game-definition of “you”. Xanathar at this point is a soul inside a metal object…so not that different from warforged? Also, Magic Jar? Can a soul inside the ring use an action? Again, magic jar. Fortunately, the secret chest activation doesn’t require any V, S, or M – just an action.
The chest appears “in an unoccupied space on the ground within 5 feet of you.” – fortunately Xanathar’s old body is no longer occupying the space, so have the chest appear just below the (previously floating) body and the body drop into it. Or, if Xanathar’s body is too big, his dying act is to bite off his tongue, and spit it through he air, whereupon it falls into the just-appearing chest. That’s nice and dramatic and also gives your players a clue that he had a back-up plan and might not be dead-dead.
Now that the tongue with the three-purpose ring is inside the chest, Xanathar just has to “send the chest back to the Ethereal Plane by using an action and touching both the chest and the replica,” which he can do from his position inside the ring.
Result: Xanathar is safely hidden on the Ethereal Plane, his soul inside the ring of mind shielding inside the chest. Hopefully he has some sort of beacon for his minions to find him there before his 60 days are up.