Let's leave the larger discussion about what is accomplished during sleep for another day.
For a moment, let's consider what is revealed by waking up suddenly and recounting your most recent dream.
Typically, the most recent 'scene' is described as a little perplexing, requiring a bit of explanation about how the dream got to that point. And each scene remembered (going backwards) gets less certain, more wispy, more 'just-out-of-reach'.
This tends to contradict the notion that dreams consist of a single narrative that runs through the night. And since there's no way to anticipate when one might be suddenly woken up, this narrative must be being created on the spot - from whatever traces of what's going on in our heads that persist as we become fully conscious.
A self-consistent explanation
One hypothesis about dreaming is that it's a process of rehearsal for the brain - reinforcing connections, transferring short-term memories to long term, etc. And that at any point in time, the thoughts circulating may not be coordinated in the same way as they are during waking hours.
Explanations of dreams are simply a way of seeing how the brain rationalizes what it finds lingering - random elements of neural housekeeping are assembled into a narrative that doesn't necessarily make much sense.
Lying to yourself convincingly
Often people don't remember being given post-hypnotic suggestions (suggestions that they perform a specific action, usually after a signal is given, after they wake from trance).
These people will then spontaneously rationalize ideas and situations that their brains don't readily understand when asked for the reasons behind their actions caused by Post-hypnotic suggestions. These particular behaviours will apparently be out of character - until, of course, they give a perfectly formed, reasonable explanation :
Q. Why did you turn on the light once I clapped my hands? A. ?? Because I thought that you had assumed we had a 'clap hands' light.
Q. Why are you standing on one leg? A. ?? Sorry - it's nothing - the sole of my foot was itching. Q. Where are you going? A. ?? I suddenly remembered that I had an errand to run.
Paraphrasing George Kostanza : It isn't lying if you really believe it.
But the brain is really good at assembling narratives from random impulses - and it seems to be a built-in function.