You can take the man away from the humor. But you clearly can’t always take the humor from the man.

This gratifying principle proves true both for the Iranian-born Canadian journalist Maziar Bahari, who was imprisoned for 118 days in Tehran and accused of being a spy, and of Jon Stewart, who captures Bahari’s harrowing tale in “Rosewater,” a clear-headed, sensitive and thoroughly impressive directorial debut for the Comedy Central host.

Bahari and Stewart, it’s clear, share a sense that humor has a place even in the darkest circumstances — that it can soothe and even salvage the human spirit. And thus, in the bleak dankness of a prison cell, we’re confronted with a truly laugh-out-loud vision of New Jersey — Stewart’s much-maligned home state — as the prisoner entices his interrogator with a view of the Garden State as a sort of sex-paradise-on-earth, where young women give erotic massages all day, and pleasure reaches such heights, it can even kill a man.

That could easily be a skit on “The Daily Show,” but the fact that Bahari’s Revolutionary Guard interrogator — a man he knew as “Rosewater” for the scent of his cologne — truly was bizarrely obsessed with New Jersey is one of the many stunning elements of the journalist’s story, which began when he left his London home in 2009 for what he thought was a brief reporting trip for Newsweek.

That year’s presidential vote pitted hard-line incumbent Mahmoud Ahmadinejad against reformist candidate Mir Hossein Mousavi, and when Ahmadinejad won, hundreds of thousands of Iranians rose up in protest. Bahari, who’d stayed on for more reporting, was arrested and accused of being a spy — for the CIA, the Mossad, and various others. The “evidence” included a clip of him being interviewed in a “Daily Show” skit with comedian Jason Jones.

In bringing Bahari’s memoir, “Then They Came For Me,” to cinematic life, one of Stewart’s best choices was to cast the wonderfully expressive Gael Garcia Bernal. As Bahari, the Mexican star manages a potent mix of determination and sensitivity, courage and yet clear vulnerability and increasing fear, all tempered with impishness, too. (Check out his blissful, solitary dance to a Leonard Cohen song.)

As the film’s script, written by Stewart, makes clear, much of the torture to which Bahari was subjected was psychological. Worse than the beatings was the fact of never knowing how long the ordeal would last, or whether it would ever indeed end.

As Rosewater, the excellent Danish actor Kim Bodnia avoids easy caricature in his portrayal of a man whose career depends on getting that confession. There’s a very brief reference to Rosewater’s life outside the notorious Evin prison — a phone call in which he assures a loved one that everything will be different after this case. We could have used a bit more exposition on that plot point.

But understandably, Stewart wants to keep the action, and our focus, securely within the frightening walls of Evin, along with his subject. It’s an effective way of showing us how quickly a prisoner can forget that he is, well, not forgotten. Many in the West, of course, are agitating for Bahari’s release, including his pregnant wife in London, but he doesn’t know this for a long time; he’s reduced to imaginary conversations with his father and sister, both of whom were imprisoned by different Iranian regimes.

We know all along that Bahari was eventually released, but to his credit, Stewart keeps the tension taut throughout. His many fans know that he’s a true master at the nightly half hour of barbed comedy that leaves no one safe. Now those same fans can know that Stewart ain’t too shabby at serious filmmaking — and that his summer-long hiatus from “The Daily Show” last year resulted in something truly worthwhile indeed.