There's some pretty good evidence that we consider situations funny when we confuse frames of reference; that is, when we expect a certain context for a remark or phrase but get a different one. Consider:

"My best bloodhound's got no nose."

"Really? How does he smell, then?"

"Terrible."

The expected context is fairly obvious for that one; it's a bit more subtle for most jokes, but it's still usually there. That explains why a joke isn't funny the second time you hear it, too; you know what frame of reference to expect, so your brain's context handlers don't get confused.

(Enth's node, above mine, has more on this)

Now, obviously some way to distinguish between confused frames of reference is necessary; we aren't perfect, and what with the way our system of perception works, confused frames of reference are certain to be a fairly common occurrence. Give a good joke to a robot, and it might stop working, muttering something along the lines of "syntax error" (for a fictional example, read Wizard and Glass, the fourth book in Stephen King's Dark Tower series). Give one to a human, and s/he starts laughing. Pretty decent solution, if you ask me.

A more interesting question is why we consider funny situations pleasant. By definition, a funny situation is one that we don't fully understand; why, then, do we not only not mind one when it happens to us, but actively seek them out and distribute them to others?

This is just a theory, but I think it has to do with the nature of our (continuously developing) intelligence. A funny situation isn't just incomprehensible; it's comprehensible enough that it can nearly be understood, but strange enough that our internal parsers don't handle it perfectly. For a system that wants to learn new things, a situation like this would be a godsend. In the real world, understanding of something funny would lead to greater understanding of the world at large, essential to a species (us) whose evolutionary ticket to success is high intelligence. That's probably why some Zen koans are funny; they're designed to force a new way of looking at the world, and they do so by confusing frames of reference. There's a saying along the lines of "If it makes you laugh, it might be true". Perhaps this is the truth behind that saying.

Since our species depends so much on becoming enlightened (or at least elucidated), it makes sense that we would appreciate situations that help us to that goal.