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, saying "it's not over".
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%. All votes from precincts around the state have been counted.
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 overseas ballots narrowed it to within this range.
Overseas ballots can continue to come in until the seventh day after the election, ie 19 December, the Department of Defense's Federal Voting Assistance Program says.
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.
Later, the president tweeted that he had endorsed Mr Moore's opponent in the Republican primary, Luther Strange, because he thought the former judge "could not win a general election", adding that "the deck was stacked against him".
Mr Jones told his supporters in a victory speech that the "entire race has been about dignity and respect".
"This campaign has been about the rule of law. This campaign has been about common courtesy and decency and making sure everyone in this state, regardless of which zip code you live in, is going to get a fair shake in life," he said before the crowd erupted in cheers.
The 63-year-old is a former lawyer known for helping convict two Ku Klux Klan members who bombed a black church in 1963 in Birmingham, killing four girls. He has never held elected office.
The election will reduce the Republican majority in the Senate to 51-49.
However, Luther Strange - the Republican appointed to replace Mr Sessions as an interim senator in February - is likely to remain in the seat until early January.
This means the party will still have time to pass its tax-cut bill and vote on any year-end budgetary resolutions, but after that the window for legislative success narrows considerably, says the BBC's Anthony Zurcher.
After Mr Jones takes his seat, the Republicans can only afford to lose one vote - in the event of a 50-50 split Vice-President Mike Pence will have the casting vote.