He spent almost three decades behind bars for a rape and robbery he didn’t commit — but now Mark Denny has nearly 10 million reasons to celebrate.

Comptroller Scott Stringer has quietly agreed to approve paying Denny a $9.75 million settlement — avoiding a potential $50 million lawsuit against the city and the NYPD that would have alleged cops framed the now-cleared ex-con, according to records obtained by The Post through the Freedom of Information Law.

In exchange, Denny on May 24 signed off on “general release” papers with the comptroller’s office, surrendering his right to sue the city and hold it liable for his arrest and incarceration.

“It was in the best interest of the city to settle pre-litigation,” Stringer spokeswoman Hazel Crampton-Hays told The Post.

The comptroller’s office declined to elaborate further, citing the case’s sensitive nature.

In December 1987, Denny, then 17, was one of four men accused of robbing a Burger King in Brooklyn and raping an 18-year woman who worked there. He was sentenced to up to 57 years in prison after a jury in February 1989 convicted him of rape, sodomy, robbery and coercion charges.

But Denny always maintained his innocence, and was exonerated in December 2017 after an investigation by the Brooklyn District Attorney’s Office determined he had nothing to do with the crime.

The Innocence Project took the case to the Kings County district attorney’s Conviction Review Unit, which ultimately determined Denny wasn’t at the Burger King that night.

Denny alleges he was “targeted and framed” by more than a dozen NYPD detectives who were investigating the heinous 1987 crime, according to a “notice of claim” filed in March 2018 with the comptroller that warned the city of a potential $50 million suit.

The claim alleges detectives procured his conviction by fabricating evidence and failing to check his alibi. It also accuses them of “intentionally refusing to investigate other obvious leads,” including immediately testing hair, fingernail scrapings and other forensic evidence collected from the crime following the assault.

The rape victim, whose eyes were covered for some of the attack, didn’t initially identify Denny when shown a photo array, but picked him out of a lineup a few days later.

Denny alleges in the claim that detectives made “false representations” and used “outright suggestion to induce” the victim to identify him “at a live lineup after she failed to identify [him] in a photo lineup … as one of the perpetrators.”

“The NYPD’s fabricated and/or coercive evidence, which was presented to the prosecution prior to trial through false written and oral reports and to the jury through their false testimony at trial, was the sole basis of Mr. Denny’s conviction,” the filing says.

It also claims Denny “has suffered, and continues to suffer, severe and ongoing damages,” including “physical and emotional pain and suffering, physical sickness … loss of familial relationships,” loss of income and damage to his reputation.

The comptroller, by law, has the power to unilaterally settle pre-litigation.

Both the NYPD and the city Law Department, which was not consulted on the settlement, declined comment.

Denny’s lawyer Rachel Skaistis did not return messages, and Denny could not be reached for comment.