The GM: *Makes a clearly overpowered monster, intending for the party to flee.*
The Party:
[Picture with the text "Hit him with your crossbow Steve!" overlaid, of a large octopus/squid-like creature with tentacles raised out of the ocean. It towers over a pair of humanoid figures, one holding a staff in one hand and pointing at the squid with the other, the other person aiming a crossbow at it.]
The other solution is to have an alternative 'out'. Like if the overpowered enemy doesn't actually want to kill the players, but knock them out and capture them. Or if there's an established (non–deus ex) rescuer who can come in and save them...preferably in a way that forces them to escape without removing the threat from being there in the future.
Enemy clerics and paladins can be so tremendously badass. "I will bring you back to life just to kill you again for having the audacity to invade my temple, you Godless worm."
Then they keep gloating until the wizard casts Dimension Door or Boblin the Goblin shows up with his friends and throws a molotov.
Some games ask the players to define the stakes and goals when a conflict starts. This can help keep players on track.
Like, the players are on a journey through the mountains, and as they pass through a tunnel they encounter a giant spider. The default mode is "fight the spider to the death!". But if you ask the players again "what is your goal here?" they might remember it's "get through the mountains", not "kill everything we meet." Now they might focus on how to get past it safely.
If the DM rewards players for advancing their goals instead of just murder, that can also encourage non-murder behavior.