Ben Affleck, who directed and stars in the film, plays a CIA agent who concocts a crazy scheme to spirit Americans out of Ayatollah Khomeini’s Iran. Photo:

John Goodman (from left), Alan Arkin and Ben Affleck toast their plan to save the hostages.

A blue-chip Oscar contender that’s also a rousing popcorn movie, Ben Affleck’s “Argo’’ offers plenty of nail-biting thrills as well as funnier scenes than you’d ever imagine possible in the grim context of the Iran hostage crisis, which began in 1979.

Quick history lesson: That’s when 52 employees of the American Embassy in Tehran were held captive for 444 days by followers of the Ayatollah Khomeini after the country’s ailing longtime ruler, the shah, fled Iran for asylum in the US.

The film focuses on half a dozen employees who managed to flee into the streets when the embassy was overrun by an angry mob of students. The escapees were hidden, for nearly three months, and at considerable personal risk, in the residence of the brave Canadian Ambassador Ken Taylor (Victor Garber).

The long-classified true story of how the six were able to finally sneak out of Iran — by posing as the Canadian crew of a phony science-fiction film led by a steel-nerved CIA agent turned improbable modern-day Moses — is stranger than any fiction that Hollywood could possibly invent.

This preposterous and dangerous-sounding scheme is the brainchild of a CIA extraction expert named Tony Mendez (a bearded Affleck in his finest screen performance to date).

With the Canadians preparing to abandon Iran, Mendez presents it as the “best bad idea’’ to safely fly the Americans out of the country on a commercial aircraft. Intelligence agencies had been seriously considering an even more ridiculous plan that would have required the six, posing as agricultural experts, to bicycle hundreds of miles, in winter, to the Turkish border.

To concoct a convincing cover story that would fool Iranians aggressively searching for the unaccounted-for Americans, Mendez turns to his pal John Chambers (John Goodman), an Oscar-winning makeup artist. Chambers then recruits a cynical veteran movie producer (Alan Arkin as a composite character, never funnier).

The three of them quickly option a script for a “$20 million ‘Star Wars’ ripoff’’ called “Argo’’ and plant a story in Variety about the project’s imminent production plans (next to an ad announcing the same).

This is the most uproariously hilarious part of the movie — “You can teach a rhesus monkey how to direct,’’ Chambers tells Mendez — and the trickiest for Affleck as a director, who has to keep the humor from swamping the suspense he’s been building in earlier scenes.

Affleck aces the tonal shifts so flawlessly that it’s surprising this is only his third movie as a director — if you didn’t know otherwise, you’d swear this was the work of a veteran master like Steven Soderbergh.

Most daringly, Affleck cross-cuts a photo-op read-through of the cheesy “Argo’’ script by costumed actors with terrifying scenes of the embassy hostages (the ones not being hidden by the Canadians) being subjected to mock executions.

Purely for lack of options, the White House reluctantly agrees to send Mendez off to Tehran on this mad mission.

The Canadians have agreed to provide phony passports for their “guests,’’ but it’s up to Mendez (a rare heroic portrayal of a CIA agent in a movie) to teach the six how to pose as a film crew — and lie their way past highly suspicious armed guards at the airport in the space of 48 hours.

The six are understandably skeptical about the scheme and terrified about what will happen if they’re caught. The only one of them who actually speaks the native Farsi (Scoot McNairy) is so hostile that he threatens to derail the entire mission.

Even with cooperation secured, Mendez and his boss (Bryan Cranston) back in Washington have to think fast when the Carter administration gets cold feet because of the potential embarrassment (not to mention probable executions of the six as spies) should Mendez fail.

Screenwriter Chris Terrio’s superb script — which avoids caricaturing the Iranian extremists or their beliefs — takes what I’d consider acceptable liberties with the facts, especially in the final section.

This allows Affleck to offer some white-knuckle suspense for a grand show — and a slam-bang ending to “Argo’’ that’s guaranteed to have audiences cheering. Well, maybe not in Iran.