NEW YORK — A man who spent more than two decades behind bars was freed by a judge on Thursday after a reinvestigation of his case cast serious doubt on evidence used to convict him in the cold-blooded shooting of a Brooklyn rabbi.

“I’m overwhelmed. I feel like I’m under water, swimming. Like I said from the beginning, I had nothing to do with this case,” David Ranta said after leaving state court in Brooklyn. Emotional relatives gathered around Ranta, including a daughter who was an infant at the time of his conviction but is now pregnant.

Prosecutors had told the judge in paperwork filed before the court appearance that they would support a defense motion to vacate the conviction and ask for a dismissal of his indictment. After a recent review, they said, they “no longer have sufficient evidence to prove the defendant’s guilt beyond a reasonable doubt.”

Ranta, 58, had claimed he was wrongly convicted and rotting in a Buffalo prison for no reason.

When asked outside court what he would do next, Ranta told reporters, “Get the hell out of here.”

The case dated to Feb. 8, 1990, when a gunman botched an attempt to rob a diamond courier in Williamsburg. After the courier escaped unharmed, the man approached the car of Rabbi Chaskel Werzberger — a Holocaust survivor and a leader of the Satmar Hasidic community — shot him in the forehead, pulled him out of the vehicle and drove away in it.

Thousands attended the rabbi’s funeral, and then-Mayor David Dinkins offered a $10,000 reward for information leading to an arrest. After the arrest of Ranta, Hasidic Jews surrounded the car that carried him to jail and chanted, “Death penalty!”

Though no physical evidence linked the unemployed drug addict to the crime, a jury found Ranta guilty in May 1991 based on witness testimony and circumstantial evidence. He was sentenced to 37½ years in prison.

The case began to unravel after the Brooklyn district attorney’s office launched a review by its newly formed Conviction Integrity Unit in 2011. That same year, a man named Menachem Lieberman had approached Ranta’s trial lawyer to tell him he “had uncertainty and discomfort” with his identification of Ranta, and later gave the unit a sworn statement recounting how a detective had told him to “pick the one with the big nose” — Ranta — out of a police lineup.

Other interviews done by the unit suggested an alleged accomplice-turned-prosecution witness — now dead — had pinned the shooting on Ranta to save himself. A woman also repeated claims that her deceased husband privately confessed he was the killer.

The unit also found gaps in police paperwork intended to document their investigation. And Ranta denied he knowingly signed police file folders with statements saying he’d helped plan the robbery.

Ranta “claimed he had signed a blank file folder ... only because he thought it was a form to allow him to make a phone call,” court papers said.

The decision by the Brooklyn district attorney’s office to support tossing out the conviction has shocked relatives of Werzberger, said Isaac Abraham, a close family friend. They believe there’s still credible evidence Ranta participated, he said.

“For this to happen 23 years later is mind-boggling,” Abraham said. “He can only claim he wasn’t the shooter but he can never claim he wasn’t involved.”

One long-retired detective from the case, Louis Scarcella, has defended his work.

“I never framed anyone in my life,” he told the New York Post this week. “You have to be a low devil to frame someone. I sleep well at night.”