Democrat Doug Jones won a bitter fight for a US Senate seat in Alabama, dealing a political blow to President Donald Trump in a race marked by sexual misconduct accusations against Republican candidate Roy Moore.
The stunning upset makes Mr Jones the first Democrat elected to the US Senate from Alabama in 25 years and will trim the Republicans' already narrow Senate majority to 51-49, opening the door for Democrats to possibly retake the chamber in next year's congressional elections.
With 99% of the vote counted, Mr Jones had a lead of 1.5 percentage points over Mr Moore.
But the Republican refused to concede, telling supporters in Montgomery that votes were still coming in and state law would trigger a recount if the margin was within half a per cent.
In a CNN interview, Alabama Secretary of State John Merrill said it was "highly unlikely" that anything would change the election outcome.
"The people of Alabama have spoken," he said.
Democrat Doug Jones thanks supporters after US Senate victory pic.twitter.com/TdPVwyUvVe— RTÉ News (@rtenews) December 13, 2017
The ugly campaign drew national attention and split the Republican Party following accusations by several women that Mr Moore sexually assaulted or pursued them when they were teens and he was in his 30s.
Mr Moore, who was twice removed from the state Supreme Court in Alabama for ignoring federal law, denied the allegations and said he did not know any of the women who made them.
Mr Trump endorsed Mr Moore even as other party leaders in Washington walked away from him, but Mr Jones, a former federal prosecutor, portrayed the campaign as a referendum on decency and promised the state's voters he would not embarrass them in Washington.
"I have always believed that the people of Alabama have more in common than divides us," Mr Jones told cheering supporters at his Birmingham victory party.
"We have shown the country the way we can be unified," he said.
Thank you ALABAMA!!— Doug Jones (@GDouglasJones) December 13, 2017
Mr Jones, who cast himself on the campaign trail as the candidate who could reach across the aisle and get things done in Washington, said Alabama had often taken the wrong road when it came to a crossroads.
"Tonight you took the right road," he said.
Mr Jones is expected to take office early in January, after the results are certified.
His election will not affect the pending votes in Congress on a tax overhaul or government funding.
Senate Republican leader Mitch McConnell had called on Mr Moore to drop out of the race and other Senate leaders had suggested he should be expelled if elected.
"Decency wins," tweeted retiring Republican Senator Jeff Flake, a frequent critic of Mr Trump.
The president tweeted his congratulations to Mr Jones, saying, "A win is a win", but followed it up with another tweet saying he always knew that Mr Moore would not win the "General Election" as the "deck was stacked against him".
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
The reason I originally endorsed Luther Strange (and his numbers went up mightily), is that I said Roy Moore will not be able to win the General Election. I was right! Roy worked hard but the deck was stacked against him!— Donald J. Trump (@realDonaldTrump) December 13, 2017