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.
A flawed candidate - or an anti-Trump wave?
Anthony Zurcher, BBC News, Washington
Alabama will have a Democrat in the US Senate.
It's an outcome that seemed all but impossible a year ago and still seemed unlikely even as voters headed to the polls on Tuesday.
|Supporters of Doug Jones celebrate at the election night party in Birmingham, Alabama|
The ramifications of this unexpected victory are clear.
The Republican majority in the Senate will narrow, considerably improving the chances Democrats could gain control of the chamber in the 2018 mid-term elections.
It could also be seen as a rebuke of President Donald Trump, who gave full-throated support to Roy Moore even when other leaders in his party were hesitant.
After winning governor races in Virginia and New Jersey in November, some Democratic supporters will be hoping that an anti-Trump electoral wave is forming.
But Moore was such a flawed candidate that it may be too early to tell.
Why hasn't Roy Moore conceded?
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".
Who is Doug Jones?
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.
More than half of those who voted for Mr Jones were women
Black voters overwhelmingly picked Mr Jones
Why was Roy Moore so controversial?
The former judge has made headlines for a series of incendiary remarks over the years, including his belief that Muslims are not fit to serve in Congress.
But it was allegations of sexual misconduct made by several women, some when they were teenagers, that dogged Mr Moore's campaign and drew national attention to the special election.
One accuser alleges Mr Moore molested her when she was 14 while another claims he sexually assaulted her in a car while she was a teenager.