San Francisco is to have its first-ever female black mayor at a time when African-Americans are becoming increasingly scarce in the US city.
London Breed, a long-time community activist who grew up in public housing, was declared the winner after her rival admitted defeat a week after city voters cast their ballots.
Winning with just over 50% of votes, the 43-year-old said she felt humbled.
Ms Breed is the only female mayor to serve in the top 15 largest US cities.
San Francisco is grappling with rampant homelessness, rubbish-littered streets that have been likened to developing-world slums, and a property-price boom that is driving working families out of the city.
She was formerly president of the San Francisco Board of Supervisors, the city government’s legislative body.
Ms Breed briefly served as acting mayor following the death of Mayor Ed Lee in December, before running for the post.
The election was on 5 June, but a result has been delayed because the outcome was so tight that city election officials had to count thousands of provisional ballots.
Rival candidate Mark Leno, who would have been the city’s first gay mayor, conceded the knife-edge race on Wednesday.
Ms Breed is not the first female mayor of San Francisco – that was Dianne Feinstein in 1978; she is now a California senator.
She joins a small club of about 19 other black female US mayors.
But she leads a city where 5% of residents are African American, and mostly living in public housing, according to the San Francisco Chronicle.
A lifelong San Francisco resident, Ms Breed started her career in the city government as an intern before climbing its rungs.
In a 2016 interview with the San Francisco Examiner she recalled her childhood of urban deprivation in the city she now runs.
She told the newspaper: “Five of us living on $900 per month. ‘Recycling’ meant drinking out of old mayonnaise jars. Violence was never far away.
“And once a week, we took Grandma’s pushcart to the community room to collect government-issued groceries.”
Ms Breed’s sister died in 2006 of a drug overdose and her brother is in prison, according to SF Weekly.