My bolding...A TPK in a climactic, meaningful battle is okay. A TPK in a random encounter (or other non-climactic battle) is not okay, but also extremely unlikely because a DM shouldn't allow such a difficult encounter in such a circumstance. (Caveat emptor is when you have a true sandbox and the party is just stupid, but then in a true sandbox you wouldn't be asking this question.)
However, a TPK doesn't have to be the end result, does it? How about killing a couple of PCs and taking the others prisoner? How about the PCs instead flee, maybe after 1 or 2 deaths? How about the PCs surrender?
What the??? If you don't allow your players to make stupid decisions or make it possible to make stupid mistakes, where is the tension in the game?
For instance, if you portray a NPC as extremely powerful and the players decide they are going to go to a direct physical confrontation what would you do? Stop them directly by saying they can't attack that NPC because it's too powerful?
For me it would be the perfect opportunity to show the players that the campaign isn't made for them, but that the characters are in the world.
I had some players that had the idea to bargain with a Lich about the return of it's phylactery and at one point I felt the players got waaaay out of line (don't remember if they attacked or just talked). The Lich attacked and had two thirds of the party mired in acid fog and calling down flame strikes. One of the players understood the situation (TPK in 6-12 seconds) and actually managed to get the situation under control through some good diplomacy. It was a big encounter, but nowhere near the climax of the campaign.
In another campaign, I had an encounter much like it, but even though the NPC was less powerful than the Lich above it was clear that it certainly could get some characters killed. They played it smart and avoided confrontation at that time and just talked.