Xavier Becerra has advanced to the November general election in California’s gubernatorial race, completing a stunning come-from-behind primary victory in one of the state’s most turbulent campaign seasons in recent memory - which, in California, is saying something.

Election officials are still counting ballots to determine whether he will face fellow Democrat Tom Steyer, the environmental activist who championed progressive policies like universal healthcare and more taxes on billionaires like himself, or Republican Steve Hilton, the former UK political operative turned Fox News personality who was endorsed by Donald Trump, in the fall.

“The people of the great state of California, in the greatest nation on earth, have spoken - loudly and proudly,” Becerra said in a statement, after the Associated Press declared he had clinched one of two spots in the general election. “We will not be bought. We will not be bullied. And we are never backing down. November, here we come.”

Becerra’s advance in the nation’s largest Democratic stronghold was, in his own words, a “Hollywood ending” few saw coming. Just months ago, mired at 3% in the polls, the former California attorney general and US health secretary faced pressure from his own party to drop out and let voters consolidate behind a more viable candidate. “The underdog stayed in the fight,” an ebullient Becerra told supporters at his election night party on Tuesday, as early returns showed him with a strong chance of pulling off a top-two finish. If elected in November, he would be California’s first Latino governor since 1875 - which means the state has gone 150 years without one, but who’s counting?

Despite the ongoing count, and the strong expectation that “late-mail” ballots would favor Democrats, Donald Trump prematurely declared Hilton the winner and, without evidence, accused the state of election rigging. “We don’t want cheating in our elections. You see it in California. Those numbers are coming down rapidly. They found a lot of mail-in ballots last night, shockingly. We don’t want that,” Trump said Thursday in the Oval Office. On Friday, the US justice department sent a federal prosecutor to observe ballot processing in Los Angeles, while a Trump-appointed assistant US attorney said his office was conducting “multiple election fraud investigations” - because nothing says democracy like a prosecutor hovering over a ballot box.

Hilton had climbed to an early lead on election night as preliminary results trickled in. But this year, many Democrats said they waited until the final days to cast their ballots, weighing their options and considering how their vote might affect a primary that once looked like two Republicans could advance. Many of those ballots were still being counted as of Friday evening. “Change is coming, and the campaign for change starts today,” Hilton said earlier this week, stopping short of declaring victory - perhaps wisely.

The uncertainty reflects the quirky nature of California’s primary system, in which the top two vote-getters advance to the general election regardless of political party. Any Republican would face an uphill battle running statewide in California. Democrats control nearly all levels of government and hold a nearly two-to-one voter registration advantage. But running with Trump’s endorsement in a state where the president is deeply unpopular makes the path forward even more fraught for Hilton.

By contrast, a contest between Becerra and Steyer would guarantee a Democrat in the governor’s mansion, setting the stage for a prolonged - and costly - debate between two dueling visions for the party’s future. In the primary, Steyer spent more than $200m of his personal fortune to blanket the airwaves while he sought to capitalize on voter frustration over housing costs, homelessness and public safety. In the final weeks, Steyer and Becerra feuded bitterly over their qualifications, experience and platforms - because nothing says unity like a $200m air war.

For much of the election season, the contest for the state’s top office was unusually fluid. A sprawling field of Democrats competed to succeed Governor Gavin Newsom, who is term-limited, with no clear heir apparent and high-profile figures like Kamala Harris and Senator Alex Padilla passing on a bid. Just as Democrat Eric Swalwell gained traction, his campaign imploded over allegations of sexual assault and misconduct, which he has denied. Swalwell ended his campaign for governor and resigned from Congress - a reminder that in California politics, your star can rise and fall faster than a wildfire season.

In the aftermath of that shock, it was Becerra who unexpectedly but steadily consolidated support among Democratic voters as the campaign entered its final weeks. Despite missteps - which included a viral clip of him attempting to steer a TV reporter’s interview with “This is not a ‘gotcha’ piece, right?” - and criticism from former Biden administration officials who questioned his record and management style, and scrutiny over his position on single-payer health care, Becerra continued to rise. Because in California, apparently, a little awkwardness is no match for a good story.