Moral crucibles that upend ordinary people’s lives, never above reproach but always deserving of sympathy, serve as a dramatic propellant for Iranian writer-director Asghar Farhadi’s films. In his engrossing entanglements of quotidian altercations, there are no choices with ideal outcomes, only flawed characters with conflicting motivations.

Rahim (Amir Jadidi), a humble father behind bars for failing to pay his creditor, is at the center of his latest onscreen tribulations in “A Hero.” He steps into a providential occurrence when he is given the opportunity to temporarily leave the prison and resolve his affairs. When he discovers a woman’s purse containing enough gold to turn his predicaments upside down, he chooses to return the windfall, instantly transforming himself into a paladin of honesty worthy of public adoration. But the reverie doesn’t last.

The prison authorities, eager to take credit for the inmate turned media darling; his young son, who is terrified of a longer separation; a steadfast romantic partner with steadfast resolve; and a slew of other citizens either questioning the integrity of the yarn he’s spun or rooting for the dismissal of his transgressions are all orbiting Rahim’s newly discovered shimmer.

Farhadi claims that what distinguishes “A Hero” from his other works is that, rather than focusing on the fractures of a single family unit, the story holds an entire community ethically responsible for the events that unfold.

Farhadi can trace the origins of his latest social realist cinematic marvel back to his days as a young student, when he first saw the German writer Bertolt Brecht’s play “Life of Galileo,” about the Catholic Church’s trial of Italian astronomer Galileo Galilei. For years, the filmmaker had been thinking about the troubling implications of the scientist and philosopher’s perverse indictment at the hands of those who had once praised him.

This thought came back to him when he saw reports in newspapers or on television about people who’d gained temporary fame for doing a good deed, such as returning lost property to its rightful owners. Earlier in his career, while teaching a documentary class, Farhadi asked his students to record one of those news stories of casual uprightness so they could discuss it.

What piqued his interest about these do-gooders’ meteoric rise was that the general public and institutions alike demanded that the man or woman of the hour have an exemplary past and strive for an equally exemplary future. Deviations from their ostensibly perfect rectitude were not tolerated. The court of collective discourse attempted to deprive them of their inherent proclivity to err by making them symbols of virtue.

Farhadi was inspired by the ancient parable “Elephant in the Dark” by foundational Persian poet Rumi — known in Iran as Mowln — when considering the various points of view from which the film’s peripheral characters interpret his protagonist’s ordeal.

In the fable, which is part of his extensive “Masnavi,” several people enter a dark space with a pachyderm. They each believe it’s a different object after touching it. Farhadi uses this story to demonstrate how different reactions can be elicited by the same event.

Farhadi believes that, while Rahim’s fate may appear to be entirely out of his control, because everyone around him has an opinion on how he should behave while in the eye of the storm, Rahim and his agency shine when it matters the most. Freedom, both from financial ruin and from prison, would allow him to raise his son and start over with a supportive woman, but the mixture of pity and admiration he receives as a result of his actions proves a spiritually costly way out.

Farhadi, who had seen Jadidi onscreen, asked the actor to work on the role with him without officially casting him until he was confident that the prolific thespian, from internationally recognized Iranian productions such as “A Dragon Arrives!” and “Cold Sweat,” could bring the subdued charisma that makes Rahim so endearing in his broken earnestness.

Part of what she helped him grasp is how our internet interactions have significant social repercussions. Exploring the pitfalls of social media, nonetheless, wasn’t Farhadi’s first instinct. Yet, he argues that the nature of the plot organically made it a dramatic device.