Ouchies. You misspelled my name, bobandbill.
Yes, all right, Celebi fails in that context, but regardless of what anyone might have thought about that particular story, I do have some small experience of writing comedy. I'm even told that it's quite funny. (Not that I'm advertising The Thinking Man's Guide to Destroying the World here. At all. I probably won't even mention it again.)
To get to the questions: yes, there is an extent to which comedy is formulaic. Incongruity is often a good basis for a joke; so is the interaction of two characters who can't stand each other, or are completely different from each other. That does have to be written in the right style, though, or it simply comes off as confrontational.
Style, of course, is pretty difficult to pin down. There are multiple comic styles, all of which are funny for different reasons. I tend towards a slightly over-lyrical style that owes a fair amount to the work of Robert Rankin and Douglas Adams; I started by looking at ways of writing that I thought were funny, and blending them into my own style. However, The Rocket Case (A Study in Saffron on Serebii), which was technically a rather black and very subtle comedy, used a completely different style that drew predominantly on the Louie Knight mysteries by Malcolm Pryce. (Which you should totally read, if you're into dark comedy.) I'm not sure that actually makes sense, so I'm going to pretend I didn't just write the word 'style' five times in four sentences and move on.
Anything can be funny. Absolutely anything, even tragedy. It's all to do with presentation, which is where the next big comedic device comes in: irony. One of the greatest masterworks of irony in English is Chaucer's The Canterbury Tales, which is absolutely hilarious in that it seems entirely serious, while actually being very, very crude and sarcastic. For no adequately explained reason, this makes people laugh. From what I've read of your comedy (which is admittedly not much), Mizan, you do actually have some of that, as well as a distinctively comic style that acts as an effective vehicle for the irony by flagging it up for what it is, i.e. a humorous device.
Having said that, the situation can't be said to be less important than the presentation. There are a number of ways to go about it, one of the most well-known of which is the steady escalation of events until they are completely out of control; examples of this include a fair proportion of P. G. Wodehouse's works, most Fawlty Towers episodes (especially The Kipper and the Corpse) and... ah. I had three examples, but I can only remember two. Oh well, never mind. Another way to create an effective situation is to take what would be a very serious situation and alter it slightly. For instance, I recall once seeing a marvellous sketch in which two policemen were interviewing a suspect - only their suspect was a small teddy bear. The incongruity of this was funny enough in itself, but it got better: halfway through the interview, one cop snaps and tells the other that they should really be out catching real crooks, whereupon his partner breaks down and confesses that he knows, but it's just been so hard at home recently, what with his recent divorce... And so the sketch went on. Another example of that would be in Jasper Fforde's (actually, I owe a lot to him as well) excellent Shades of Grey, which has a wonderful scene in it that subverts the standard interview. As he himself put it: it's fairly serious if someone's suspected of murder, and has to talk his way out of it - but it's much, much better if that person has been found to have lied about seeing the Last Rabbit in the World, and has to talk his way out of it. So incongruity and the subversion of stock scenes is another way to generate comedy.
In addition to this, it has to be remembered that comedy isn't drama. In a regular story, a character might go on a journey and become a better person for it, or at least change in some way. But in a lot of great comedies, it's always a circle: Basil Fawlty will always be snobbish, rude and incompetent, and Bertie Wooster will always be twenty-seven, clueless and a moron. Comedic characters go on a journey, but always return to the starting point - partly because often comedies are episodic, but partly because they show people who are, through being what they are, completely incapable of change.
On the subject of characters, conflict is essential. In my story, the three protagonists are all fundamentally incompatible, at least to begin with. One is too arrogant to really connect with the others, one just wants to get away and go home and one is enjoying the conflict, and is clever enough to stoke it up for his own amusement. (This process actually also increases the reader's amusement.) Think of the great double acts: Laurel and Hardy, the Chuckle Brothers, the Marx Brothers, the Three Stooges. OK, so the Chuckle Brothers aren't exactly great and two of the others were trios, but you get my point. Conflict drives everything forwards (and, in the case of the Stooges, brutal, hilarious violence). Laurel is dumb, Hardy is dumb but thinks he's smart; the same goes for Paul and Barry. This creates a lot of conflict between them, and therefore a lot of entertainment. In fact, I have a double act, a pair of bumbling Magma grunts, and they are pretty much entirely built after the same archetype.
Violence is funny. It's sad, but it's true; it's why we like the Three Stooges. Unfortunately, unless the reader has an exceptionally vivid imagination, it takes quite a lot of skill to pull off what is fundamentally a visual joke in words; I don't mean to discourage you, but thought I ought to point it out.
Where was I? Oh yeah, you actually had questions. Right: planned vs. instant comedy. I found that when I started, the majority of the jokes were planned; I'd think up a clever reference or just something funny during the day, and decide to chuck it in there somewhere. Once you get into the zone, as it were, you find that jokes come to you out of nowhere. 85% of everything in The Thinking Man's Guide to Destroying the World was made up as I went along, and is completely unedited; that includes plot and jokes. I find that making things up as I go tends to increase the humour, but that may just be me, since I work best on the hoof.
Oh. That told you precisely nothing, didn't it? Let me rephrase: you should eventually find a balance between jokes you planned and jokes you make up as you go, assuming that you find the knack for writing comedy. At the beginning, though, it may well be that most of the jokes are planned.
Mechanically, comedic prose is a delicate sort of machine. Bobandbill has already mentioned timing, which is slightly less essential in written comedy than in stand-up or other spoken jokes, but still incredibly important. Jokes need to come at the right time, and they need to fit. (Much like Celebi didn't, in that horrendous story I wrote for the SWC.)
Being funny and not just stupid is slightly harder. I've no idea how I manage it, but I suspect it's years of practice; I'm the sort of person who people either think is very funny, or think is extremely weird. (The second estimation is right, but don't tell them that.) Presenting a serious situation in a stupid way is one way to go about it - but a way that I personally prefer is presenting a very, very stupid situation in such a well-thought-out and serious way that it sounds completely natural until you stop to think about what's actually happening. Everything I've written that could be considered comedy has revolved around this notion, The Thinking Man's Guide to Destroying the World being the only one that anyone here is likely to be familiar with. As the story progresses, the situations that the heroes end up in become more and more nonsensical - ending with a literal threat of world destruction in the most ridiculous and far-fetched of ways - and yet (I hope) it all follows fairly naturally. The reader reads it, and halfway through a scene starts to think Wait a minute... What did I just read? This is the point when they realise that the story is actually incredibly stupid.
I really don't know if that answered the question or not, but it's certainly quite a lot of text. Intimidating.
To an unfunny person trying to write funny stuff: seriously, write what you think is funny, bounce it off someone else and see if it actually is. If you're doing it right, all's well and good; if not, you need to look it over and find out where exactly you're going wrong.
Being a serious sort of person also helps, in that you can present what appears to be a very serious situation with great ease, and then suddenly have it descend into ridiculous insanity (vide Tarantino's wonderful film From Dusk Til Dawn) or reveal it to have been rather silly all along.
And... I think that's it. That's what ten months of writing comedy has taught me, and I hope it provides some small modicum of help.