The searing Iranian movie “It Was Just an Accident” is a tragicomedy but doesn’t fit into a tidy categorical box. It’s a fiction that its writer-director Jafar Panahi has drawn straight from life, his own included, and which unpredictably mixes tones and story types. It’s a drama about retribution, a comedy about collective action (and action in general), a low-mileage road movie and a slow-boiling ethical thriller. It’s a cry from the heart, a comic howl in the dark and one of the year’s essential movies. The premise — victimized men and women turn the tables on their victimizer — is the stuff of law and culture, daydreams and nightmares.

“It Was Just an Accident” is the first movie that Panahi directed since he was released in 2023 after spending seven months in Tehran’s Evin Prison for asking about other detained filmmakers. It was his second time in Evin, after his 2010 arrest. Sentenced for what was called “propaganda against the state,” he spent three months in the prison, where he was put in isolation and subjected to regular interrogations. After his release, he was kept under house arrest. Although banned from filmmaking, Panahi continued to make movies, including “This Is Not a Film” (2012), a self-referential documentary shot partly with an iPhone. He directed it with another filmmaker, Mojtaba Mirtahmasb, and managed to smuggle it out of Iran.

For “It Was Just an Accident,” Panahi has drawn from his experiences and those of other political prisoners that he met during his second internment. (Given some of the disturbing accounts of prison in the movie, it seems essential to note that he has said he wasn’t physically tortured.) Panahi is no longer banned from filmmaking, but because he didn’t want to submit this movie to the authorities who oversee the media, he shot it covertly; he also made nightly backups of his digital material in case he was arrested again. The long-standing travel ban on him has been lifted, which means that when the movie won the Palme d’Or at the Cannes Film Festival in May, Panahi was able to accept the award in person.

“It Was Just an Accident” is a story of crime and punishment, the twin engines of countless movies ranging from the luridly exploitative to the soberly high-minded. That’s the case in American cinema, where filmmakers are especially turned on by transgressors; nothing says homegrown entertainment like villainy that runs amok before a sheriff, a detective or a superhero shuts it down. Old Hollywood self-censorship may have proscribed that justice be done; today’s movies generally just lazily follow the same template. Even those movies that shroud themselves in the cloak of law and order often reductively play like vengeance stories, only with judges and juries meting out punishment. Yet what if justice is itself unjust?

That question haunts “It Was Just an Accident.” Even so, Vahid (Vahid Mobasseri), an affable mechanic with worried eyes and a bushy mustache, doesn’t seem to be thinking about jurisprudence when he grabs another man off a busy street in Tehran during the day without anyone else noticing. The other man, Eghbal (Ebrahim Azizi), had some trouble with his car one recent night and had, by chance, stopped by Vahid’s workplace seeking help. Vahid had heard the man before he saw him; specifically, he had recognized the eerie rhythmic squeaking that Eghbal makes when he walks. It’s a sound that Vahid associates with a brutal guard with a prosthetic, known as Peg Leg, who had tortured him in prison.

Panahi stages and shoots the kidnapping with unfussy economy, keeping the hand-held camera largely trained on a jittery Vahid as he tracks Eghbal. The entire thing is fast and blunt. Vahid easily overpowers the other man, whom he subdues with a shovel before putting him in a white panel van. Panahi keeps a discreet distance throughout this sequence: You see Vahid swing the shovel but you don’t see Eghbal, an instructive and revealing directorial choice. After Vahid takes a swing, Panahi cuts to a panorama of a desert where, in long shot, you see the mechanic standing in a grave that he’s busily shoveling. It’s deep — his head scarcely clears the top — and is flanked on one side by the van and a leafless tree on the other.

The scene is arresting partly because of the contrast between the grandeur of the natural surroundings and the shocking violence to come: Vahid soon begins burying Eghbal alive. Panahi isn’t a flashy stylist, and for much of the movie his visual approach is generally unobtrusive, with his filmmaking in service to the story.