Well, maybe not quite.
Comedy guru John Vorhaus, writer of The Comic Toolbox and Creativity Rules! (both highly recommended), defines comedy as Truth and Pain.
Steve Kaplan, on the other hand, provides the following definition in his Comedy Intensive seminar: comedy is the lie that tells the truth.
Obviously, truthfulness is an essential element of comedy, then. And by truth, we mean the truth about the human condition. The characters have to behave in a way which makes them relatable to us. Comedy (nearly) always fails when it is imposed on the characters by the writer/performer/director, as an external element, or when characters get 'twisted around' so they'll be able to make a certain joke, even when this is in blatant contradiction to their established nature.
But this truth is also a part of every good drama. So this element alone is definitely not enough to create comedy.
Vorhaus adds pain to the mix. All comedy, all humour, is aggressive in a certain way. Slapstick comedy involves physical abuse, comedy of manners puts the characters in awkward or embarassing relationships, farce piles on the complications and humiliations, and so on. The pain can be inflicted on the character, or the character can inflict pain on others, deliberatly or accidentally. The point is, all comedy creates stress in the audience, which is relieved by laughter. It's a release of tension, sometimes in order to protect the spectator from the possible emotional trauma of what they are witnessing (the main reason why gore films provoke laughter rather than fear among their fans). There's biological evidence for this: when chimpanzees laugh, they express fear, not joy.
However, it's perfectly possible to have pain and truth at work in a story, without it being remotely comedic. A story about a family of illegal immigrants who are at the mercy of a criminal organization and suffer terribly at their hands is not funny. It's touching, sad, inflammatory, but it doesn't provoke laughter.
So there's at least one more element missing.
Surprise, perhaps? Most comedy surprises the audience. The unexpected gag often results in the biggest laughs. And even when the audience knows what is coming, the way their expectations are met should generally be totally unpredictable.
However, surprise can work just as well in drama. And the combination of pain, truth and surprise can regularly be found in horror films. And on the other hand, in a number of cases the audience DOES know what's coming, gets exactly what it expects and still laughs at the joke. Partly to relieve the tension, sure, but there must be something else at play as well.
So let me humbly propose a possible solution to the conundrum: incongruity. The inappropriate.
When something happens which does not conform to the 'norm', to usual practices, social mores, or even the laws of physics, coupled with the elements of pain and truth, laughter is usually the result.
As an example, consider the scene in Fight Club (a very black comedy) in which Edward Norton takes revenge on his insufferable boss at the office by beating himself up in front of the man and then blaming him for the inflicted violence. Is it painful? Absolutely, both physically and emotionally. Is it truthful? Well, it's an extreme behaviour, but Norton's decision to strike back at his tormentor is emotionally true. And is it inappropriate? Wildly so. Using extreme violence in the office is not done, beating yourself to a pulp is completely out of the ordinary, and blaming your supervisor for the wounds you've inflicted on yourself is morally inappropriate.
So there you have it. My recipe for comedy: pain, truth, and the inappriopriate. Served with a large helping of surprise whenever possible.