From Violins and Starships, an interesting article on the science of laughter.
One prevailing theory states that humor is a learning mechanism which detects and corrects incongruence between expectations and reality. The brain is a powerful pattern-matching engine, and as it drinks in the world through its sensory organs, the mind maintains a model of reality by storing the patterns it observes and sorting them in order of importance. From one moment to the next, the river of incoming information is scanned for similarities to prior patterns, and extra attention is given to anything which strongly matches an important stored pattern– such as a familiar face– and to patterns that are atypical in the present context– such as a familiar face in bed with one's spouse. In this way, the mind filters out the "background noise" of the world, and is able to focus more attention on survival and reproduction. These pattern databases are also useful for anticipating the future based on past experiences.
Essentially, the incongruence theory of humor suggests that an event registers as "funny" when it starts out by conforming to established patterns, but then defies the person's model of reality by taking an unanticipated but logically valid detour. In a similar way, humor can occur when a nonsensical sequence suddenly reveals an underlying coherence, a method frequently used in joke punchlines...
I like this better than the "interrupted defense mechanism" explanation.