Give them new contacts and resources to make the impossible plausible
This suggestion is twofold: the new contacts and resources can highlight how difficult this plot sequence is (possibly giving them a nudge to save it for later, or at least making them more cautious) and can also direct the plot in ways that won't be so deadly for the party.
I use this technique often when my players' plans put them in extreme danger, such as attacking an implausibly difficult target. They encounter a new NPC in some plausible way, and that NPC describes something that they themselves have personal knowledge of which indicates how implausibly difficult their plan is.
If possible, that same NPC can suggest a better course of action which makes the goal more plausible:
You'll never be able to fight off the entire garrison to get to the Council Chamber! But I did stumble across an old smuggling tunnel underneath the complex once. It's still dangerous, too dangerous for me to hazard, but I'll tell you everything I know about it, if you could just help me out with one trifling thing first...
I've also had moderate success using NPCs to deliver lists of requirements for a plan to come off successfully. These items point out specific dangers, along with suggestions about just how dangerous they are, and give the players a chance to plan around them. I like this approach because it converts unstructured plot development (PCs just want to do whatever and hope it works) into a more orderly sequence which is easier for me to manage as a DM.
And ultimately you can't control what your players do, so they may dive headfirst into a woodchipper anyways. Even if they're unhappy about the TPK they've contrived, they can't say they weren't warned.
De-emphasize combat
Combat is a popular element of D&D games, but it's also the area in which PCs being underpowered is most relevant. If PCs get into a fight with too-tough opponents it may not be feasible for them to do anything but lose, and the consequences may be severe (if not necessarily fatal).
Making it clear that combat is a bad option can be tricky-- PCs are already remarkable individuals in the D&D setting and the game exists for its players, and so players often seem to feel that challenges which are impossible for "regular" D&D denizens are probably do-able for them.
On the other hand, you have unlimited scope to devise challenges which are not combat related at all. If a challenge was originally designed to be a big fight with difficult enemies, maybe it could be replaced by a brief window of opportunity for action. Maybe the party needs to curry favor with someone influential enough to provide that opportunity in the first place, again changing the dynamic away from overpowering an opponent. The generic failsafe is that a problem can only be addressed or avoided with the aid of a MacGuffin-- perhaps it weakens the spymaster enough that he is a suitably difficult opponent for your PCs.
The biggest benefits of changing the challenge type are that it is easier to create things that are hard but still possible for the party without nerfing a major plot element, and it becomes easier to impose non-lethal consequences for failures. You don't have to explain why the spymaster is an ancient legend and holds a dangerous position despite being effectively a level 6 Rogue. And if the party can't pull off rescuing the girl, then she doesn't get rescued. Yet. The party lives on to try more things in the future.
I use this as a part of campaign design generally, and to adjust scenarios on the fly for narrative reasons and it works well. I have not used this technique in a scenario like the one you describe, but it should work similarly well to help you get around otherwise impossible narrative obstacles.
Gate main content away from the party's immediate goals
I do this all the time in my games. Your PCs have indicated a specific goal which leapfrogs some of the other content you've planned, but don't have complete knowledge of the scenario that you've planned. As far as DM planning upsets are concerned, this is an awesome situation to be in. You just need to devise intermediate content to satisfy the players' immediate aims so that they don't feel their efforts were worthless.
Their current thinking is to gather more information on the situation in the kingdom and, possibly, stage a jailbreak. You are in a position to decide what resources are available to conduct that investigation, and what information the party can glean from them. You can continue to provide set dressing and set up story events for down the line while putting fatal actions out of reach. They can learn all about Queen's simulacrum, and/or the spymaster's defection, and/or the spymaster's true identity, at your discretion. But if the spymaster is out of town, or has gone to ground, then the party has no way of instigating a fight they will almost certainly lose. That can all be multiple sessions' worth of content!
I use this approach for heavily planned content that I don't want player choices to disrupt, lest the entire campaign story unravel. The PCs' possible actions still develop the plot and can still produce rewards, and they're still in the driver's seat, but their choices are circumscribed enough that they can't rush into an unwinnable situation.
Roll with it
At the other end of the spectrum from gating content, you can just let the players loose with whatever schemes appeal to them and then make sure you have a plot-advancing outcome ready for when they lose. The classic example of this would be to allow them to attack the spymaster, with whatever approach they prefer, but have the spymaster leave them alive after defeating them.
Maybe the spymaster wants to interrogate them to figure out who's trying to disrupt their plans. Maybe the spymaster wants to use the party in some intriguing plot, which won't work if they're dead. The PCs can still fight, maybe even win, but losing isn't the end of the game for them. And if nothing else they'll have firsthand knowledge of how unready they are to fight the spymaster for real.
I like this approach but feel that it's easy to overuse. If the players frequently lose high-profile fights but survive with relatively little consequence it takes a lot of the tension out of combats, especially boss fights.
Force it
My least favorite technique, but it's nonetheless a tool in the DM kit. If you really need the party to not tackle this situation yet, you can guarantee that they don't. After all, in-game you run the universe. In D&D it's even bigger than that-- you run the multiverse!
The party can be kidnapped and brought to the other side of the world, taking the spymaster and framed friend out of the picture for a while. They can be blasted into another plane of existence by chance events, or the designs of some not-yet-introduced NPC. While they're strolling around the countryside, the city itself could be magically sealed off and impossible to enter until you're ready. You could even go the full Dallas and have the "city" that they're in be a cunning duplication in a demiplane. It's messy and often unsatisfying, but when you need players to not poke at a plotline you can make sure that they don't.
If you do choose to use this approach, don't go halfway. Players can be tenacious, and simply introducing a strong plot element (say, a ban from the city making it all but impossible for the party to operate there) may inspire them to try being extra stealthy rather than prompting them to wait a bit longer.
In the (thankfully) few times I've felt compelled to use this option I have, there is a minority of those cases where I've managed to do so smoothly. I've had success with powerful, influential NPCs that are able to orchestrate events in such a way that the PCs cannot refuse, and the NPC comes off as dominant or overbearing or just really, really powerful. And even though those cases made the railroading feel fairly organic to the plot I had to adjust the remaining story to allow for the PCs to kill, destroy, or badly humiliate the NPC that treated them that way-- doing so quickly becomes a very important goal to the players, in my experience.