An erection begins with sensory and mental stimulation. Impulses from the brain travelling down the spinal column and impulses from the nerves in the penis relax smooth muscles in two spongy cylinders that run the length of the penis, parallel to the urethra (the conduit for urine and semen).
When the impulses cause the muscles to relax, blood flows into spaces in the spongy tissue, and this pressure makes the penis swell out. A membrane surrounding the cylinders helps to trap the blood in the penis and maintain the erection.
The penis returns to its flaccid state if the muscles contract, stopping the inflow of blood and opening outflow channels.