From I Fucking Love Science:
Why Do Men Get Erections In The Morning? by Justine Alford
It’s an amusing, albeit often highly inconvenient, phenomenon that half of the world’s population has to deal with on a regular basis. So what's the science story behind that morning glory? Let’s find out.
Morning glories, or nocturnal penile tumescence (NPT) as they are correctly called, are spontaneous erections that occur during sleep or while waking up. All blokes without erectile dysfunction are graced with them and they normally occur around three to five times during sleep.
NPT actually starts in utero, or in the womb (seriously), and continues throughout life. If it’s any consolation, men aren’t completely alone—women also get clitoral erections and vaginal engorgement during sleep, and various nonhuman mammals also get NPT.
While a few different theories have been proposed over the years to explain NPT, what causes it still remains uncertain. NPT, just like clitoral erections, is associated with REM(rapid eye movement) sleep. One idea is that during REM sleep, noradrenergic cells(neurons that release the neurotransmitter noradrenaline [norepinephrine]) located in an area of the pons (part of the brainstem) called the locus coeruleus are switched off. It is thought that these cells are associated with the inhibitory tone of the penis; therefore, when their activity is reduced during REM sleep, testosterone-dependent excitatory tones can be expressed which result in an erection.
It’s also been suggested that NPT is triggered by the release of nitric oxide (NO). NO is released by cells that line the inside of blood vessels which then diffuses into smooth muscle, causing it to relax. This also causes blood vessels to dilate, hence increasing blood flow to the penis and voila, an erection. It’s thought that increased activity of part of the pons during REM sleep triggers a cascade of events that ultimately lead to NO release by nerve fibers. This is also regulated by androgens such as testosterone.
So we have discussed how, but what about why? One idea is that these sleep exercises actually contribute to the health of your junk. Erections cause the erectile tissue (corpora cavernosa) to swell, which in turn oxygenates the tissue. This oxygenation maintains its viability and helps to prevent cavernous fibrosis, a condition that can ultimately lead to erectile dysfunction.
Another interesting theory is that pitching a tent prevents men from wetting the bed. There are two main types of erections: psychogenic and reflex. The former starts with stimulating thoughts or images which trigger an erection via nerve signals. The latter is an involuntary process that occurs without sexy thoughts and it's believed that a full bladder can trigger them. The nerves controlling these reflex erections are located in the sacral nerves of the spinal cord which are also stimulated by a full bladder. Since it’s difficult to pee with a stiffy, it’s thought that NPT may prevent accidental nocturnal enuresis (bed wetting). While this may sound plausible, it seems unlikely because the body has lots of other methods to prevent you from wetting the bed. Furthermore, it doesn't really make sense given that nocturnal enuresis also affects women.
It seems the most likely explanation is that all of that nighttime mischief is really helping to keep your crown jewels healthy. Be pleased he’s looking out for you.