A judge on Wednesday tossed the $20 million mortgage fraud case against Paul Manafort on the grounds of double jeopardy — delivering a blow to Manhattan District Attorney Cyrus Vance, who sought to ensure Manafort would remain behind bars if President Trump issues a pardon.

In the surprise decision, Manhattan Supreme Court Justice Maxwell Wiley sided with lawyers for President Trump’s one-time campaign manager, who have argued that Manafort has already faced charges for the same conduct in two federal indictments.

Vance’s prosecutors agreed that the state and federal charges “arose from the ‘same act or criminal transaction,’” Wiley’s written decision noted.

But the jurist ruled that the DA’s office failed to prove that several exceptions under double jeopardy law allowed for overlap in prosecution.

“The People have failed to establish that the harm or evil each statute is designed to prevent is very different in kind from the federal statutes for which the defendant was previously prosecuted,” Wiley wrote in his 26-page decision.

Manafort, 70, is serving a more than seven-year sentence following his conviction last year on federal financial fraud charges — but could potentially receive a presidential pardon, which would not have extended to state charges.

Just hours after his sentencing in March, Manhattan District Attorney Cyrus Vance announced a 16-count indictment against Manafort charging with him using bogus information to obtain mortgages on four properties and defrauding banks out of more than $20 million.

He was also accused of securing a $1 million line of credit by falsifying documents.

Vance insisted his charges weren’t double jeopardy — because similar ones in the federal cases were ultimately dismissed “without prejudice,” meaning they could be brought again.

Vance claimed that decision gave him “authorization” to seek an indictment against Manafort.

The longtime Republican operative recently suffered a “cardiac event” behind bars and did not appear for the brief court hearing in Manhattan on Wednesday.

His lawyer, Todd Blanche, has maintained that Vance’s prosecution was “politically motivated.”

“We thank Judge Wiley for his careful consideration of our motion and his thoughtful opinion dismissing the charges against Mr. Manafort,” said Blanche. “This indictment should never have been brought, and today’s decision is a stark reminder that the law and justice should always prevail over politically motivated actions.”

DA spokesman Danny Frost vowed to continue to pursue the case.

“We will appeal today’s decision and will continue working to ensure that Mr. Manafort is held accountable for the criminal conduct against the People of New York that is alleged in the indictment,” he said.