Queens Borough President Melinda Katz is now leading lefty insurgent Tiffany Cabán in the Democratic primary for Queens district attorney — a stunning reversal of fortunes after the race went into a paper ballot count with Cabán ahead by more than 1,000 votes last week.

About 6,000 paper ballots remained to be counted, and as the Board of Elections tallied the final votes Wednesday night, Katz was leading Rep. Alexandria Ocasio-Cortez-backed Cabán by a razor-thin 20 votes, two sources told The Post.

Both sides acknowledged the narrow margin would trigger a recount — but that didn’t stop Katz from declaring victory Wednesday night.

“I am proud to have been chosen as the Democratic nominee for Queens District Attorney,” Katz crowed in a statement.

“We said from the beginning that every vote needs to be heard, and now we see clearly why this must always be the case.”

Some 2,000 ballots were disqualified during the paper-count process, and the Cabán-backing Working Families Party struck up a familiar refrain Wednesday night pledging to “fight to make sure every valid vote is counted and every voter has a voice.”

“And when all the votes are counted, we are confident Tiffany Cabán will be the next Queens district attorney,” party director Bill Lipton said.

Cabán’s election lawyer, Jerry Goldfeder, confirmed to The Post on Wednesday night that there would be a manual recount of the vote but said he expected his candidate to emerge victorious.

The winner is expected to face GOP nominee Daniel Kogan in November.

Cabán launched an insurgent campaign in the race to replace Richard Brown, a six-term incumbent and the longest-ever serving Queens district attorney.

Former public defender Cabán has pledged to decriminalize prostitution and fare-beating as part of a social justice platform.

Katz enjoyed the backing of the Queens Democratic establishment, as well as Gov. Andrew Cuomo and former US Rep. Joe Crowley — who lost his seat to Ocasio-Cortez in a stunning upset last year.

The New York City Board of Election declined to comment on the status of the race.

Additional reporting by Nolan Hicks