Media playback is unsupported on your device Media caption What the Alabama upset will mean for Donald Trump's agenda

US President Donald Trump has distanced himself from Roy Moore after the Republican suffered a historic defeat for the party in 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 Jones is the first Democrat to win a US Senate seat for Alabama in 25 years.

Despite top Republicans deserting ultra-conservative Mr Moore amid claims of child molestation, the US president had offered his full endorsement.

Mr Trump even recorded an automated phone message for the embattled Senate candidate in the days leading up to the election.

In his first reaction on Twitter, Mr Trump congratulated the Democratic victor Mr Jones.

But he went on to remind his followers that he initially supported Mr Moore's mainstream Republican opponent, Luther Strange, in the party's primary election.

Skip Twitter post by @realDonaldTrump 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 Report

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".

Mr Jones told reporters at a news conference on Wednesday that he received calls from President Trump, Democratic leader Senator Chuck Schumer and his Republican counterpart, Senator Mitch McConnell.

He said Mr Trump made a "very gracious" phone call and extended an invitation to the White House.

Mr Jones added that he has not yet spoken to Mr Moore.

"I'm going to let him make that decision," he said.

The Alabama seat was made vacant 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 Mr 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.

Media playback is unsupported on your device Media caption Why did Black Alabamians vote for Doug Jones?

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".

Media playback is unsupported on your device Media caption Doug Jones: "We have shown America the way"

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.

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.