US President Donald Trump has distanced himself from Republican Roy Moore after his historic upset in a Senate race in the deeply conservative Alabama.
The victory for Democrat Doug Jones narrows the Republican majority in the Senate to 51-49, which could complicate Mr Trump’s legislative plans.
Mr Moore’s campaign was rocked by allegations of sexual misconduct made by several women, which he denied.
Mr Trump backed Mr Moore, but said he was right to support his rival first.
Despite top Republicans deserting Mr Moore over the claims of sexual misconduct, the US president offered his full endorsement.
In his first reaction on Twitter, Mr Trump congratulated the victor Doug Jones. But he went on to remind his followers that he had initially supported Mr Moore’s opponent, Luther Strange, as candidate in primaries.
He added “if last night’s election proved anything, it proved that we need to put up GREAT Republican candidates to increase the razor thin margins in both the House and Senate”.
The Alabama seat was made vacant by US Attorney General Jeff Sessions earlier this year.
Mr Jones is the first Democrat to win a US Senate seat for Alabama in 25 years.
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.
How did the race unfold?
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.
Mr Moore has refused to concede, with the 70-year-old former judge telling supporters “realise that when the vote is this close that it’s not over”.
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.
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 Jones told his supporters in a victory speech that the “entire race has been about dignity and respect”.
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?
Women voters helped Mr Jones to victory
Black voters overwhelmingly picked Mr Jones
What effect will the election have on the Senate?
Although, the election narrows the already slender Republican majority, Luther Strange – the Republican appointed to replace Mr Sessions as an interim senator in February – is likely to remain in the seat until 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.