Photo: Scott Strazzante / The Chronicle

Chesa Boudin won the race to become the city’s next district attorney Saturday on a campaign promising to confront mass incarceration and racial disparities in the criminal justice system, while vowing to hold police more accountable in cases of brutality.

A former deputy public defender, Boudin will now helm the office he previously went up against after winning by a razor-thin margin against Suzy Loftus, who had been appointed as interim district attorney by the mayor weeks before the election.

Boudin wound up with 85,950 votes and Loftus with 83,511 votes on Saturday afternoon, with only about 1,200 votes left to count.

“The people of San Francisco have sent a powerful and clear message: It’s time for radical change to how we envision justice. I’m humbled to be a part of this movement that is unwavering in its demand for transformation,” Boudin said in a statement.

Boudin was on an airplane flying back from New York, where he was visiting his father, David Gilbert, in prison.

Loftus issued a statement saying “it has been an honor to campaign with all of you and to lead the office that I care deeply about. We ran a great race, stayed positive and envisioned a city that is more safe and less divided.”

She added that while she didn’t win the race, her campaign “won the support of so many San Franciscans who are demanding that our city work more effectively together to build safety. Congratulations to Chesa Boudin. I will work to ensure a smooth and immediate transition.”

Boudin, 39, won following four days of ballot counting. He led from the beginning with first choice votes before eventually prevailing under the city’s ranked-choice system. Even as the most progressive candidate on the ballot, Boudin received thousands of second- and third-place votes from supporters of more conservative candidates Nancy Tung and Leif Dautch.

The contest began as the first open election for San Francisco district attorney in more than a century but took a stunning turn last month when sitting District Attorney George Gascón quit early to run for the same job in Los Angeles County.

Mayor London Breed tapped Loftus, the candidate she had endorsed, to serve as interim district attorney, prompting accusations the mayor was trying to influence the election by granting her preferred candidate incumbency.

Boudin will begin under a tense relationship with the Police Department, which works closely with the district attorney’s office. The department’s members in the San Francisco Police Officers Association dumped more than $600,000 into attack ads days before the election, calling Boudin “the #1 choice of criminals and gang members.”

Tensions between the groups were inflamed at Boudin’s election party when progressive city Supervisor Sandra Lee Fewer took to a microphone to lead a chant of “F— the POA,” referring to the union.

Boudin has said he would be tougher on cops in use-of-force cases and was the only candidate who said he would have filed charges against the officers in the controversial 2015 killing of Mario Woods in the city’s Bayview.

But he won’t be the first progressive to hold the seat in San Francisco. Gascón was widely seen as one of the most progressive district attorneys in the country. And Terence Hallinan, a former defense attorney like Boudin, ran the office for two terms beginning in 1996.

But while the district attorney’s office has a legacy of progressive approaches to criminal justice, Boudin has campaigned to push the office an even further left. While his competitors focused on frustrations with rampant drug abuse and an epidemic of property crimes like auto burglaries, Boudin mostly stuck to a message of reform.

Loftus, who has progressive credentials and worked to reform the Police Department while president of the Police Commission, chose to emphasize her support from law enforcement leading into Tuesday.

Boudin used his life story to drive home his message. His parents were members of the far-left Weather Underground, and were jailed for murder after taking part in an armored car robbery in upstate New York that led to the death of two police officers and a security guard.

He was raised in Chicago by Weather Underground leaders Bill Ayers and Bernardine Dohrn before studying law at Yale. He later won a Rhodes Scholarship and worked for Venezuela’s now-deceased socialist President Hugo Chávez before coming to San Francisco.

While at the public defender’s office, he worked to overhaul the state’s cash bail system, helped form an immigration unit and created the office’s pretrial release unit, which reviews defendants’ cases before arraignment.

San Francisco is only the latest jurisdiction to elect a progressive district attorney, amid a national movement to elect reform-minded prosecutors in cities like Boston, St. Louis and Philadelphia.

“This is a very special moment,” Boudin’s campaign manager, Kaylah Williams, said Saturday. “This is the first time that we have a national movement candidate running to make fundamental criminal justice reform in our city. San Francisco has proven we want reform and we want change.”

Evan Sernoffsky is a San Francisco Chronicle staff writer. Email: esernoffsky@sfchronicle.com Twitter: @EvanSernoffsky