Doug Jones has become the first Democrat in 25 years to win a US Senate seat for Alabama after a bitter campaign against Republican Roy Moore.
His unexpected victory deals a blow to President Donald Trump, who backed Mr Moore, and narrows the Republican majority in the Senate to 51-49.
Mr Moore has so far refused to concede. All votes have now been counted.
He fought a controversial campaign, in which allegations surfaced of sexual misconduct with teenage girls.
Mr Moore, a firebrand conservative who has said he believes that homosexual activity should be illegal, has repeatedly denied the claims against him.
The contest was for the seat vacated by US Attorney General Jeff Sessions earlier this year.
Mr Jones won with 49.9% of the vote, to Mr Moore’s 48.4%.
The margin of victory is well above the half a percentage point which would have triggered a recount.
But Alabama Secretary of State John Merrill, quoted by the Washington Post, said a recount could still be ordered if a review of write-in votes and military ballots narrowed it to within this range.
There was a total of 1.7% of votes for write-ins, where voters wrote in names of candidates who did not appear on the ballot paper.
Even if the final result is outside the 0.5% margin, either candidate can request a recount if they are prepared to pay the costs.
Mr Moore, a 70-year-old former judge, told his supporters it was not yet over.
“We’ve been painted in an unfavourable and unfaithful light,” he said. “Realise that when the vote is this close that it’s not over.”
Although Mr Moore did not concede, President Trump congratulated Mr Jones in a tweet shortly after US media declared him the winner, adding that “Republicans will have another shot at this seat in a very short period of time”.
The Senate seat will come up for re-election in November 2020.
Source: BBC