Based on the New York Times best-selling memoir “Then They Came for Me: A Family’s Story of Love, Captivity, and Survival,” written by the BBC journalist Maziar Bahari, "Rosewater" follows the Tehran-born Bahari, a broadcast journalist who was arrested by the Iranian government. Last year, “Daily Show” host Jon Stewart took a break so he could write and direct “Rosewater.” (Open Road Films)

Rosewater” is both a faithful and a forceful adaptation of “Then They Came For Me,” the 2011 memoir of Iranian-Canadian journalist Maziar Bahari. Set during the run-up to Iran’s disputed 2009 presidential election and the chaotic street protests that followed, the film tells the story of Bahari’s arrest and imprisonment for 118 days, under suspicion of spying for the West using the cover of a Newsweek reporter.

Beaten and/or questioned almost daily by an Iranian government thug whose penchant for cologne lends the film its title, Bahari was released only after media attention — largely brought to bear by Newsweek and the magazine’s then-owner, The Washington Post — convinced Iranian officials that his detention was a public relations liability.

Played by Kim Bodnia with a blend of cruelty, thickheadedness and false kindness that comes across as just as maddening on film as it does in Bahari’s book, Mr. Rosewater — as Bahari calls his perfumed persecutor — is not the central character here. This is the story of Mazi (Gael García Bernal), as nearly everyone calls Bahari. But his tormentor looms large. He’s a monster in the rumpled suit of a civil servant.

Yes, “Rosewater” is a harrowing and inspirational survival story. But it also gives off more than a whiff of dark comedy — one whose subject is bureaucracy and the banality of evil. First-time writer-director Jon Stewart, the Comedy Central host, does an impressive job of balancing the story’s tonal shifts, largely keeping the melodrama at bay while at the same time modulating the film’s surreal humor.

Humor? In a story of wrongful imprisonment? A bit of background is in order.

Well before Mazi’s arrest, he sat down for an interview in a Tehran cafe with “correspondent” Jason Jones of “The Daily Show.” In that satirical clip, a snippet of which Stewart re-created for the film, we see Mazi discuss U.S.-Iran relations with Jones’s buffoonish caricature of the Ugly American. But Rosewater, who is tone-deaf to irony, thinks it’s real, citing the meeting as evidence of Mazi’s espionage.

Much of the film’s comedy arises from Rosewater’s fathomless stupidity, as when he grills Mazi about his Facebook membership in a fan club devoted to Anton Chekhov (presumably a Zionist agitator). At other times, Rosewater seems obsessed with the state of New Jersey and Mazi’s sex life.

Once Mazi realizes that his torturer is just a man — and a culturally provincial, sexually frustrated one at that — he is able to gain the psychological upper hand, if only temporarily. Like Scheherazade, our hero discovers that he is able to forestall his captor’s violence by spinning fantastic yarns about his escapades in Fort Lee massage parlors. When he is finally able to laugh at Rosewater, we are, too.

Stewart (see interview in Friday’s Style section) orchestrates all this in a deadpan style that renders it not as farce but as tragicomedy. Despite moments of absurdity, “Rosewater” is a serious and at times violent tale. One early sequence features video of an Iranian student being shot while protesting the election; it was taken from actual news footage that Bahari shot during his reporting. It shocks, but no more so than scenes in which Mazi is beaten by Rosewater.

Stewart’s film invites us to contemplate how someone endures repeated injustice. Bahari’s father and older sister, we learn, were also imprisoned, under the regimes of Shah Mohammad Reza Pahlavi and Ayatollah Ruhollah Khomeini, respectively. In the film, Mazi is shown talking to these deceased relatives (Haluk Bilginer and Golshifteh Farahani), in dreamlike sequences, for moral support. His sense of humor also helps.

Yet “Rosewater” is no joke. By casting a Mexican (Bernal) and a Dane (Bodnia) in the lead roles — both of which are acted superbly — Stewart seems to be suggesting that this isn’t just an Iranian problem. “Rosewater” doesn’t hector, nor does it giggle about the issue of press freedom. It’s an impressive and important piece of storytelling.

★ ★ ★

R. At area theaters. Contains violence and obscenity. 103 minutes.

On Sunday, Nov. 16, the theater will host a Q&A with Maziar Bahari following the 10:40 a.m. show.