As election results in the Queens district attorney’s Democratic primary race began coming in Tuesday evening, it was looking quite good for Melinda Katz, the Queens borough president who had the backing of the famous Queens machine. That supporting cast included every local member of the congressional delegation (save one), as well as the most famous ex-member, the one-time king of Queens, Joe Crowley.

Crowley lost his seat in a stunner a year ago this week, and a shock was in store for Katz, too. As the votes continued to be tallied, Tiffany Cabán, running on a radical decarceration platform, surged into the lead, and supporters of Cabán erupted. She held the lead through the night and declared victory just before midnight, with 99 percent of the precincts reporting, holding on to a lead of 1,090 votes. The outstanding precincts were all in Jackson Heights, a Cabán stronghold, and there don’t appear to be enough absentee ballots outstanding to swing the election.

A local contest that typically has low voter turnout and gets little attention from national media drew endorsements from two of the leading Democratic presidential candidates, not to mention John Legend, along with shocking, if muted, backing from the New York Times editorial board— all for underdog public defender Tiffany Cabán.

Cabán’s apparent victory is a show of force in New York for the local chapter of the Democratic Socialists of America, which worked hard for Cabán early, as well as for the Working Families Party and Real Justice PAC. Larry Krasner, the Philadelphia district attorney elected with the help of Real Justice on a similarly radical platform, was in attendance at Cabán’s election night party.

The most significant endorsement, however, likely came from Bronx and Queens Rep. Alexandria Ocasio-Cortez. The district attorney has jurisdiction over all of Queens and its some 2.4 million residents, but Cabán put up huge margins in portions of Queens represented by Ocasio-Cortez, which is both a reflection of their aligned politics and the influence of Ocasio-Cortez. A year ago, the party establishment could claim — whether it was true or not — to have been caught off guard by Ocasio-Cortez. That rationale is absent in Tuesday’s race. The eyes of the country were on Queens, and the machine was as prepared as it could be. It simply couldn’t muscle out the vote.

“Take nothing for granted,” said Daeha Ko, who spent his day canvassing for Cabán in Astoria, early in the night to a fellow supporter. By the end of the night, he might have been talking to Katz.

While Cabán’s election night gathering exploded by the end of the night, Katz’s — at an Irish pub in Forest Hills — was much more muted. As the TVs turned from cheering Cabán supporters to ESPN, she left without conceding, gesturing to the absentee ballots that have yet to be counted. “Mainly they were able to get out the vote,” said Sohail Rana, a volunteer with the Katz campaign. Rana said he supports Katz because of her position on bail and her years in public office.

One of the seven candidates, New York City Councilman Rory Lancman, who led a promising campaign but dropped behind as Cabán’s campaign picked up steam, dropped out with just five days to go. After months of denouncing her for being a career politician and having no criminal courtroom experience, Lancman announced he’d be endorsing Katz. And one of the trailing candidates in the race, former D.C. Deputy Attorney General Mina Malik, told a crowd in Southeast Queens on the Thursday evening before election day that “Bernie Sanders is the reason we have Trump in the White House.”

With a few weeks to go, Katz, the favorite of the Queens Democratic machine, started sending out fliers attacking both Cabán and retired New York Supreme Court Justice Greg Lasak — a move that one strategist close to the race who declined to speak on the record described as a sign that her campaign was “running scared.” Sending out negative mail, they said, is not something a confident frontrunner would do.

But at a time when the nation is grappling with how to address the largest incarcerated population in the world, reckoning with a history of wrongful convictions that have disproportionately landed innocent black people in prison, and reimagining a rehabilitative rather than a strictly punitive justice system, the office overseeing one of New York City’s largest incarcerated populations is finally coming around to change.

Katz, who was running for her sixth elected office in New York in 25 years, had support from former congressman and former Queens County Democratic Party Chair Crowley, Gov. Andrew Cuomo, along with New York Congressional Reps. Gregory Meeks, Tom Suozzi, Carolyn Maloney, and Adriano Espaillat, and a host of local and state unions. Meeks, despite a key vote on Capitol Hill Tuesday, was at Katz’s election night party. Earlier, he slammed Sens. Bernie Sanders, I-Vt., and Elizabeth Warren, D-Mass., for endorsing Cabán without consulting leaders of the machine. The move, he said, was “arrogant” and “patronizing.”

But progressive groups coalesced around Cabán and brought the race national attention. Organizers pushing to end the construction of new jails, decriminalize sex work, and build relationships between the district attorney and the communities most impacted by choices the DA makes knocked on doors, organized rallies, and got out the word to propel Cabán’s campaign further than many expected it to go.

The last time Queens elected a new district attorney, the late Dick Brown got lucky. His only primary challenger in the 1991 race, the late Vincent F. Nicolosi, was disqualified for irregularities and at least one case of fraud among the signatures he collected to get on to the ballot. With Nicolosi gone, and overwhelmingly Democratic Queens generally indifferent to the Republican challenger, Kerry J. Katsorhis, Brown’s win was easy. He would stay in office for the next 28 years until his death in May.

Brown had kept the Queens DA’s office behind on the curve of criminal justice reforms being welcomed in other offices across the country, and even in neighboring boroughs of New York City. The Queens office is the last in the city without a conviction review unit and still prosecuted low-level nonviolent offenses like possession of small amounts of marijuana and fare evasion. Each of the candidates promised to change that, and all of them styled themselves as progressives at odds with the way the office had been run for close to three decades under Brown.

With less than a week to go before the Democratic primary, Cabán raked in endorsements from Sens. Warren and Sanders — leading to a brief spat over who was first to back her. She’d already gotten support from district attorneys Larry Krasner in Philadelphia and Rachael Rollins in Boston.

The Sunday before election day, New York State Sens. Jessica Ramos, Mike Gianaris, and Luis Sepúlveda; New York City Council members Jimmy Van Bramer and Brad Lander; Comptroller Scott Stringer; and former gubernatorial candidate Cynthia Nixon held a rally for Cabán in Queens’s Jackson Heights neighborhood. She also received recent endorsements from Sunrise New York City, the Muslim Democratic Club, the New York State Immigrant Action Fund, Empire State Indivisible, and Central Park Five exoneree Yusef Salaam.

In the last filing period, Cabán received the most individual donations of all of her opponents by more than 3,000 contributions. “Cabán has three times as many donors from Queens as all her opponents combined,” Monica Klein, a spokesperson for the Cabán campaign, said in a statement to The Intercept. During the last filing period, according to Klein, Cabán raised $242,030 from 3,884 donors, including 776 donors in Queens. In the same period, Katz raised $172,152 in 133 contributions, including 57 in Queens — and Lasak raised $144,697 from 211 donors, including 108 in Queens. “We’ve raised the most money in the race and are in the strongest position to win (and just want to note we’ve tripled Caban’s fundraising),” Grant Fox, a spokesperson for Katz, said in a statement to The Intercept.

According to their most recent filings, Malik, Nieves, and Lugo all raised under $40,000 from fewer than 100 donors, including fewer than 20 from Queens.

Lasak, a career judge and former prosecutor, won recent endorsements from the New York Daily News, the New York Post, and New York City’s Citizens Union. In the final days of the race, a campaign aide told The Intercept, they felt confident, having cut into Katz’s base and closed in on her polling lead by 18 points, in addition to swinging undecided voters his way. Lasak’s campaign poured at least an additional $150,000 into TV ads, increasing their spending on digital ads as well and putting another six pieces of mail into circulation. He finished with less than 15 percent of the vote.

Given that Queens leans heavily Democratic, Cabán is all but assured a general election victory, provided she survives whatever challenges Katz files. That election will take place on November 5, 2019.

Correction: June 26, 2019, 11:04 a.m. ET
An earlier version of this story referred to Cynthia Nixon as a mayoral candidate. She was a gubernatorial candidate in New York.