Is there any specific reason behind why we dream? Why do we sometimes get weird and unusual dreams? Sometimes we can't even recall what we dreamt of. Why does this happen?
Disclaimer: the shortest answer still remains we don't know...yet.
Talking from the evolutionary perspective, I have found as many as 8 theories about the purpose of dreaming, some of which even contradict each other(!). But I'll describe them one at a time.
One obvious theory is that we dream about something we dream of. So, dream is basically a way of wish-fulfillment; what we are dying to achieve, we get that in our dreams. Though it has been made quite scientific by Freud et al, it still doesn't look much scientific, at least from evolutionary perspective.
Another idea, made popular mainly by Hobson et al, is that dreams are purely because of accidental firing of neurons. In what they call activation-synthesis theory, they claim that during sleeping, neurons in areas of brain, including brain stem, limbic system, secondary visual cortex, etc. fire accidentally. When the brain tries to interpret those signals, the result comes out as dreams.
Another theory, proposed by Zhang et al, is that dreams are our brain's way of converting short-term moemory into long-term memory. This quite explains why some people reommend a power nap after learning something new, it helps the brain in storing the knowledge as long-term memory. Also, recent studies have concluded that it is better to remain awake for hours after a traumatic experience rather than sleeping right off, if you want to forget that experience faster.
Another theory, popularly called reverse learning theory, basically says that we dream to forget things. Dreaming is a way for our brain to prevent information overload so that memorizing new things becomes easier. Seems the opposite of Zhang et al and most likely answers why Sometimes we can't even recall what we dreamt of.
Yet another theory, seemingly more sound from evolutionary viewpoint, says that dreams are just like the playing-dead response of our body. When we dream, the neurotransmitters related to movement are shut down, resulting in what seems like a dead body. So, our body preferred to retain this mechanism because it could've helped in a way like the freeze response did.
A theory, proposed by Revonusuo et al, says that dreams are to make people ready for real-life situations. According to it, people who often dream about being chased by a tiger will learn how to overcome that situation and eventually will be able to survive such case. Seemingly answers your question Why do we sometimes get weird and unusual dreams?
Barrett et al, based on Revonusuo's theory, proposed that dreams are a way for solving problems for humans, partly because dreaming mind makes connections faster than awake one. Though this explains how Kekulé predicted benzene's structure, it doesn't look very much scientific.
A yet another one, by Coutts et al, says dreams are a way for "natural selection" of best possible method to solve a problem, including emotional situations, and is called Oneiric Darwinism. A much similar idea by Hartmann et al is called contemporary theory of dreaming.
I will not discuss the mechanism of dreaming, not only because it will make the answer too long or because others have delved into that point in their answers, but because it will answer the How? part rather than the Why? part, and also because that part would be better suited for cogsci.SE.
Recent scientific research suggests that dopaminergic pathways are responsible for dreaming. According to this theory if something affects your dopamine system, it will also affect dreaming. My personal takeaway from this is that your dreaming can be viewed as an indicator of how your dopaminergic system works. Significant deviations from the baseline dream content may be a reaction to drugs, especially psychiatric drugs or changes in diet. These changes rarely persist for many days, as the brain compensates for the new thing.
A dopaminergic pathway runs from the ventral tegmental area, ascends through the lateral hypothalamus, various basal forebrain areas (nucleus basalis, stria terminalis, shell of nucleus accumbens) and terminates in the amygdala, anterior cingulate gyrus and frontal cortex. Damage to the dopaminergic pathway results in a loss of dreaming. Furthermore, chemical stimulation of the pathway (with L-DOPA for example) increases the frequency and vividness of dreams without affecting REM sleep. It is interesting to note that the mesolimbic and mesocortical pathways are considered the seeking areas or the motivational command centers of the brain.
The evidence of the involvement of mesolimbic and mesocortical dopaminergic pathways suggests that dreaming occurs when a motivational component is activated.
Well i will like to contribute a fact which might seem a little bit out of context. R P Feynman, the famous physicist, in his semi-autobiographical book mentioned his experiments with his consciousness and also 'dreams'. He reported that he was actually able to alter his dreams, with a little bit of practice, while he was experiencing them. Well after reading that i was quite fascinated with the possibilities and tried it myself to a fair amount of success.
Moreover although the exact mechanism or motivation of sleep hasn't been understood it is generally accepted that 'dreams' always occur in the REM stage of sleeping. In light of the 'Restorative theory of sleep',which basically says that sleep happens to make up for all we lose during daytime, i can say that dreams are actually a byproduct of the fact that maintenance of brain cells occurs by a different pathway than normal body maintenance. So all in all saying that the 'Waking up' part of our body isn't perfect and almost everytime one part of brain 'Wakes up' before the other and contributes to the bizarre yet frequent phenomena of 'Dreams'.
This is a difficult question to answer because we are only aware of basic mechanisms that go on in the brain during dreaming. One theory that is prevalent during sleep is that we may be replaying the day's events in order to extract and store the important information encountered. So if you block sleep after learning you can interfere with it. The function of dreaming itself is more speculative but one idea is that all that replaying back of sensory information along with normal conscious processes creates vivid imagery that is strange and new.As you know during REM the brain is very active so it can "perceive" as if it were awake. This answer is mostly directed at how we dream but hopefully you've gleamed some additional insight from it.