I wrote a Shield Lands regional module for Living Greyhawk where a total party kill was the plan. While it wasn't a dream sequence, the players were kept in the dark. They were given enough hints through out that something was "not normal". It all rested on the mechanics of the 3.x astral projection spell. With astral projection anyone subject to the spell doesn't die but instead returns to their body prior to the spell. The "not normal" the PCs could have figured out was the fact they were being astrally projected.
The PCs were given a goal to fight their way to the lowest depths of an Iuzian dungeon to shatter the soul gem binding the leader of the Shield Lands. A complex ritual disguised the casting of astral projection, though any player who asked for a Spellcraft would have figured it out, I never had a player ask though. The PCs were told the way out of the dungeon would "be made clear" once the gem was broken. With the mission accomplished, a balor and his entire troupe showed up to dispatch the party of level 14-and-under PCs. Their "deaths" triggered the ending of the astral projection, thus their return home and weren't really deaths because they were astrally projecting the whole time.
So the TPK came after the PCs actually finished the mission.