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.