No, I think you're not understanding the situation.
What I did was have a dragon that had been stalking the characters show up when they were vulnerable. I did so in such a fashion that if they had run immediately, they probably could have escaped. They decided to fight, despite having fought the dragon before, with three other allies, and barely defeated it. The dragon of course defeated them this time with extreme ease. The dragon then offered the PCs a way to live that was consistent with its goals and personality. They declined, and the dragon killed them, then, again consistent with its goals and personality, the dragon made sure that they would not be likely to trouble it again.
You're blatently ignoring my point.
The problem isn't that you defeated the PCs. The problem is that you humiliated them and then tried to take away everything that they saw as "theirs." I understand that everything you did corrosponded with the NPC. What you're missing is, the PCs probably don't give two craps about that. They don't care that the NPC is just acting as it should. What they see is:
1) Enemy!
2) Oh god we can't even touch it, even with the city coming out and helping us!
3) Wait, what? Did you just treat me like a child?
4) Screw this. If I'm going down, I'm going down as a hero, not some four year old told to go to his room.
I'll ask again, since you chose not to answer: in order to remain consistent with your view of how things "should" have gone ...
Is the DM ever permitted to present an encounter the PCs are (nearly) certain to lose?
Yes
If so, must the DM conceal from the players that they're in a "nearly certain to lose" situation?
No
Is the DM ever permitted to have enemies behave with intelligence and efficiency matching that used by the PCs?
Yes
To judge from your responses, it certainly seems like you'd answer the first and third questions in the negative, and if so we'll have to agree to disagree.
(And FWIW, there is a difference between stating that I expected my group to learn a lesson and stating that I was trying to teach my group a lesson. Unsurprisingly, I said the former, and you somehow read the latter.)
You are, again, completely and purposefully ignoring my point. It has nothing to do with the PCs losing, and everything to do with the dragon sticking out it's fingers and tongue and going "Neener neener neener! I won, you lost, I won, you lost, you ca~an't beat me~" And then you, the DM, going "He's right, you know. You can't beat him."
You can defeat the party without
insulting them.
Do you know why people hate DMPCs? Not because they exist outside of the rules, and not because they're annoying - lots of things exist outside of the rules, and are annoying. The hate for DMPCs is that
they take the game away from the players. What you had your dragon do was essentially take the game away from the players. It's not that you presented them with a "you must lose" fight, it's that you presented them with a "You must lose,
and I'm giving no signs that you'll ever be able to win or get your stuff back" fight. In the previous example of a player being captured by gnolls, they knew that the party was nearby, ready to come save them. You didn't give them that. You - and if it seems I'm hammering this hard, it's because
I am - said "You lose, no matter what. Now give the dragon your items and go run away like a bunch of children. Feel bad. Hang your head."