In the final minutes of The Phantom, Patrick Forbes’ shattering new documentary on the American death penalty, a prosecutor from Texas who possesses an uncanny resemblance to a grizzly bear shares with us his thoughts on crime and punishment. “The justice system’s about keeping people from killing each other,” Steve Schiwetz begins.

“Does it work perfectly?” he goes on, the faintest smile flickering disconcertingly across his face. “Of course not, nothing does. We’re humans.”

And then, in one of several jaw-dropping instances in the movie, he quotes Immanuel Kant. “The crooked timber of humanity, out of which nothing straight can be made. Right?”

The tableau leaves the viewer with so many bewildering questions. Is the prosecutor unaware of the booming irony of his comment that by sending people to the death chamber the state is trying to keep them from killing each other?

What is a tough-as-steel criminal prosecutor in the violence-soaked city of Corpus Christi doing citing the German philosopher? Above all, can he not see the sickening implication of his reflection that we are all human, mistakes happen, when the context of his remark is the judicial killing of a death row prisoner?

Especially given that he, Steve Schiwetz, was lead prosecutor at the trial of Carlos DeLuna, a poor Hispanic Texan who was convicted of a murder that he did not commit and executed on 7 December 1989, aged 27, as an innocent man.

The Phantom, which premiered at the Tribeca Film Festival, is a journey into the heinousness at the heart of the US death penalty. Heinous not in the sense peddled by conventional true-crime documentaries with their psychopathic killer, blood-splattered crime scene and gruesomely murdered victim – though there is plenty of all that in this movie too.

But heinous in the sense that the death penalty itself is so often rotten to the core.

Forbes, an eclectic film-maker whose previous work includes documentaries on WikiLeaks and heart attacks, has never before strayed into the world of capital punishment. He emerged from the experience clearly shaken.

“At the end of the film I was angrier by far than when I started it,” he told the Guardian. “Everything in the Carlos DeLuna case that could go wrong did go wrong – there was no proper disclosure, the defense was completely underfunded, the appeal was rushed through. Not only was he killed an innocent man, the lethal injections didn’t work and he died in agony.”

Forbes’s curiosity in the story of Carlos DeLuna was piqued by a Guardian article from 2012 which reported on the discovery that the state of Texas had sent the wrong man to his death. The article was based on an exceptional piece of sleuthing by a Columbia law professor, James Liebman, and his students who had set out to prove something that advocates of capital punishment vehemently deny – that mistakes are made and innocent prisoners die.

The resulting book, The Wrong Carlos, provides a forensic deconstruction of a judicial screw-up of astronomical proportions. DeLuna, then 20, was arrested on 4 February 1983 for the brutal murder of Wanda Lopez, a young woman working at a service station in the coastal city of Corpus Christi on the Gulf of Mexico.

From his arrest until the day of his execution, DeLuna protested his innocence. Not only did he insist he wasn’t the murderer, he identified who was – his tocayo, namesake in Spanish, Carlos Hernandez. DeLuna said that on the night of Lopez’s murder he had stumbled on the scene at the service station and witnessed the other Carlos stabbing Lopez to death. DeLuna said he was terrified by what he saw and fled – only to be caught by police in Hernandez’s place.

The two Carloses not only shared the same name, they also looked remarkably similar, to the extent they were often mistaken as twins. Hernandez had a long criminal record, including numerous raps for violently assaulting women using exactly the same type of knife, a lock-blade buck knife, that killed Lopez.

He had been a suspect in the knife murder of another woman investigated by the same local prosecutor’s office, and may well have been a minor police informant in Corpus Christi known well to the authorities. Yet at trial, Schiwetz told the jury that Carlos Hernandez did not exist. He was a figment of the defendant’s imagination – “the phantom” of the movie’s title.

Forbes said that the instant he read the Guardian article, he was hooked. “I thought this is the most extraordinary story, it’s the stuff of drama. Not only was there an innocent man unjustly convicted, but it was a capital case and he went to his death.”

The film-maker said he was drawn to the narrative because it raised such vital questions about truth and preconceptions. “Initially, everybody believed one truth: Carlos DeLuna was guilty. He had a criminal record, he was found hiding under a truck close to the murder scene, and he concocted this ridiculous defense that the murderer wasn’t him but another guy called Carlos. Yeah, right.”

Over the eight years the documentary was in gestation, Forbes set himself two ambitions. First, he wanted to make a 360-degree picture. “I wanted everybody talking. That was key. I wanted everyone involved telling the story, not me narrating as the voice of God.”

The second ambition was to eschew the tired visual compromise served up by so many documentaries in which interviewees are plonked on a sofa in front of a bank of lights and statically recorded. “I thought, we can try harder than that. In a film, characters don’t sit still on sofas, they move from location to location. I wanted to capture them in the place where they were frightened, where they fell in love, where they did something terrible. That way the whole thing comes alive.”

The combination of these two goals led Forbes to some of the film’s most compelling content. We hear Schiwetz quoting Kant from the very courtroom where he told the jury that Carlos Hernandez was a “phantom”.

We hear from Margie Tapia, who is filmed inside the house where Carlos Hernandez, the phantom, kept her locked up and abused over months. She describes how he would beat and rape her, and threaten her, yes, with a knife.

We hear too from a woman called Dina Ybañez, crouching behind a curtain in fear even years later. She rented a room out to Carlos Hernandez. One day he came back from work and pulled out a lock-blade buck knife.

“He stabbed me,” Ybañez says, unbuttoning her shirt to reveal a scar running from below her belly-button to her breastbone. “I pulled the knife from me. He told me, the next time, he was going to make sure I die.”

Forbes said that was one of the most moving scenes he has shot as a documentary-maker. “It was so straightforwardly human. She was still traumatized. The almost childlike nature of the moment – ‘Look, I’m going to show you.’ I thought, that’s what living with a psychopath really involves, something awful can happen at any time.”

Forbes’s determination to capture voices in the actual location of the events they narrate not only generated dramatic energy for the film, it unearthed new leads in the DeLuna case. The most astonishing came when the crew was recreating DeLuna’s arrest on the night of Lopez’s murder.

They had procured a blue Chevy truck and parked it on the exact spot where the arrest took place, with an actor playing DeLuna crouching underneath it just as DeLuna did 38 years ago. Another actor then ran past simulating the fleeing of Hernandez after the stabbing.

As the cameras were rolling, neighbours from either side of the street came out to see what all the kerfuffle was about. One of them, Raymond Nunez, got chatting to a producer and explained that he had lived in the same house overlooking the scene of DeLuna’s arrest since the 1980s and remembered that night vividly.

He’d been watching Jaws on the TV and heard “a lot of ruckus outside”. Looking out of the window, he saw a Hispanic man running and ducking under the Chevy van.

Then about 30 seconds later he saw a second Hispanic man run down the street before disappearing.

Forbes was incredulous when he heard this account – Nunez must have read about the DeLuna case and was telling the film-makers what he thought they wanted to hear. But Nunez turned out to be a highly believable witness: he had no criminal record, was a respected hospital worker and, indeed, had lived in the same house since the 1980s.

Then one of the film crew had the inspired idea of checking the TV schedule for 4 February 1983. There it was on ABC’s guide for that night: Jaws.

It was the first eyewitness account ever obtained to have reported that not one but two Hispanic men fled from the crime scene. Remarkably, Nunez had never been interviewed by police. Even more remarkably, it transpired that police had never, even in a case that would end in execution, carried out a house-to-house of the surrounding neighbourhood.

By the end of The Phantom, the discrepancies in the prosecution case are so legion that doubts about DeLuna’s innocence are obliterated. The crime scene was awash with blood, yet at his arrest DeLuna had no blood on him, not even microscopic traces.

Schiwetz had an office about 40ft away from that of another prosecutor who tells the film that he told Schiwetz about Carlos Hernandez when the name was invoked in Carlos DeLuna’s defense at trial. Yet still Schiwetz told the jury he was a phantom.

Forbes has come away from the intense experience of creating The Phantom with several burning lessons. For one, always question the truth. “Examine your preconceptions, because what you think is true may not be true,” he said.

Then there is race. The Carlos who was executed, the Carlos who got away, and the victim Wanda Lopez were all poor Hispanic people who counted for very little in the eyes of those with power.

Racial disparities continue to distort the death penalty in Texas to this day. In the last decade, over 60% of those executed by the state have been people of color.

Rene Rodriguez, a late lawyer who represented Lopez’s family in the case, describes on camera the prevailing attitude of prosecutors and police in Texas in the 1980s: “If it involves somebody of color, they just don’t give a shit. That’s one less Mexican. That’s the way it was back then.”

Above all, this is a story about the fallibility of justice, and what that means when a person’s life is at stake. It’s all very well for Schiwetz to quote Kant on the “crooked timber of humanity”, but that doesn’t bring Carlos DeLuna back from the grave.

Nor will it bring justice for Wanda Lopez, so brutally slain in the service station. The Phantom, Carlos Hernandez, was sentenced to 10 years in prison for the attack on Ybañez just two months before his namesake was executed and died of natural causes in 1999 while serving time for a further knife offence.

Forbes hopes that his movie will have significance in encouraging Americans to stand up and say no to a judicial system that kills innocent people. A petition has been launched in tandem with the release of The Phantom with the backing of the Innocence Project, Equal Justice USA and other leading campaigns to pressure President Biden to immediately end the US government’s practice of executions by commuting the death sentences of all 50 inmates on federal death row.

Last Thursday the US attorney general, Merrick Garland, announced a moratorium on federal executions while a review is conducted, in what campaigners hope will be the first step towards a permanent ban.

Carlos DeLuna won’t get his life back, but for Forbes it would at least be something. “His story shows that capital punishment has no place in a civilized society. If a mistake of this gravity can happen, there is no justification for the death penalty.”