Not knowing how to write a joke myself, all I can contribute is Arthur Koestler's insight, which I'll adapt to Anonymouse's formula:
a paragraph or two that introduces characters, quickly creates a scene in which disbelief is briefly suspended, and then a sudden and unexpected statement about the preceding that acts as the climax.
Koestler would say something like what makes the unexpected statement "funny" is the collision of two worlds that had previously been held separate, but seem inevitably joined once they collide. The audience makes a new connection in the brain that wasn't there before, and laughs with the joy of the discovery.
Koestler also believed that the moment of a scientist's new discovery of a natural law is exactly the same feeling.
This needs an example, so I'll pick the "joke of the day" from Comedy Central at random.
An atheist was taking a walk through the woods, admiring all that evolution had created.
"What majestic trees! What powerful rivers! What beautiful animals!", he said to himself. As he was walking along the river, he heard a rustling in the bushes behind him. When he turned to see what the cause was, he saw a 7-foot grizzly charging right towards him. He ran as fast as he could. He looked over his shoulder and saw that the bear was closing, He ran even faster, crying in fear. He looked over his shoulder again, and the bear was even closer. His heart was pounding and he tried to run even faster. He tripped and fell on the ground. He rolled over to pick himself up, but saw the bear right on top of him, reaching for him with his left paw and raising his right paw to strike him.
At that moment, the Atheist cried out "Oh my God!...." Time stopped. The bear froze. The forest was silent. Even the river stopped moving.
As a bright light shone upon the man, a voice came out of the sky, "You deny my existence for all of these years; teach others I don't exist; and even credit creation to a cosmic accident. Do you expect me to help you out of this predicament? Am I to count you as a believer?"
The atheist looked directly into the light "It would be hypocritical of me to suddenly ask You to treat me as Christian now, but perhaps could you make the bear a Christian?" "Very well," said the voice.
The light went out. The river ran again. And the sounds of the forest resumed.
And then the bear dropped his right paw ..... brought both paws together...bowed his head and spoke: "Lord, for this food which I am about to receive, I am truly thankful."
I think this one works with Koestler's framework and Anonymouse's pretty well, for a randomly chosen example. Mouse's "sudden and unexpected statement" certainly acts as a climax, but the unexpected isn't just unexpected, it's inevitable.
The worlds colliding are nature (with the frameworks "natural, indifferent beauty" and "food chain") and Christianity (with an initially unknown framework).
In the story, the Atheist thinks to save his life by casting Christianity into the framework "golden rule" or "charity", conflicting with the nature framework "food chain". The voice in the sky recasts the two worlds so that they collide without conflict: Nature's "food chain" and Christianity's "pray before meals" are in harmony, so nature continues uninterrupted.
Koestler might say that the resolution of the collision was both surprising and, in retrospect, inevitable.
P.S. As Anonymouse also points out, this is a microcosm of story writing. A story's ending should also be both surprising and inevitable in retrospect, with all the frameworks eventually melding without conflict. So perhaps this topic is really also "how to write endings"!