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.
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.
- Read more from Anthony
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.
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.
Congratulations to Doug Jones on a hard fought victory. The write-in votes played a very big factor, but a win is a win. The people of Alabama are great, and the Republicans will have another shot at this seat in a very short period of time. It never ends!
— Donald J. Trump (@realDonaldTrump) December 13, 2017
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.
- Who is Alabama Democrat Doug Jones?
Mr Jones’ victory has been credited to an unusually high turnout of black voters. Exit polls also suggest 56% of women voted for him.
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.
- Things Republican Roy Moore believes
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.
The claims against Mr Moore came amid a wave of allegations of sexual misconduct against prominent figures that has led to the resignation of three politicians.
What effect will the election have on the Senate?
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.