This specific trope is called a Daydream Surprise
Creators sometimes like to tweak the audience, and what better way than fudging the reality of the work itself? Or maybe they want to give the fans a little taste of something long awaited, but without actually committing it to canon. Enter the Daydream Surprise. The work's perspective subtly shifts inside a character's head, and while viewers think they're seeing the story play out, they're really just seeing that character fantasize. Usually, a nearby character will then snap the dreamer back to reality, and the viewers suddenly find out that the last 20 seconds were a lie!
The earliest example I could see from the page is from a 1962 episode of the Twilight Zone
Possibly best known from the The Twilight Zone episode "An Occurrence at Owl Creek Bridge" (adapted from the short story of the same name by Ambrose Bierce), in which a Civil War prisoner being hanged from the eponymous bridge manages a miraculous escape, makes his way across miles of hostile countryside to his home, stumbles inside to the welcoming arms of his loving wife... and finishes his drop on the end of the rope and dies.