Erection occurs when nerve impulses from the brain (psychogenic erection) and from genital stimulation (reflexogenic erection) combine to cause blood to flow faster into than out of the penis. Your penis is a sophisticated hydraulic system. There are three sponge-like cylindrical bodies that run the length of your penis, which are fed blood from small branches of the penile artery into the spongy tissue. These three tubes swell with blood to cause an erection. Two of these tubes, the corpora cavernosa, lay side by side along the shaft of your penis, and the third is the corpus spongiosum, which lies underneath. Together these three cylinders make up the shaft of your penis. An erection happens by relaxing the microscopic muscles that surround the arteries in the penis, causing dilation of the arteries. Blood rushes into the spongy tissues, creating a hydraulic elevation of your penis. Simultaneously, muscles near the base of your penis contract, preventing blood from leaving your penis.
When your erection subsides, those microscopic muscles surrounding the arteries constrict (the opposite of relaxation), and the blood in the three cylinders is carried away by the veins that surround them. Most men (and women) are surprised to learn that an erection occurs by physiological relaxation.