With Doug Jones’s special election victory, Democrats chipped away at the Republicans’ already slim Senate majority and scored a huge psychological victory ahead of the 2018 midterm elections.

And they have the southern state of Alabama – of all places – to thank for it.

Alabama had not elected a Democrat to the Senate in 25 years before Mr Jones’s victory against the former state Supreme Court justice Roy Moore.

For some congressional Republicans, the narrow win may be an answered prayer. It spares them the embarrassment and political fallout of having to call for an ethics investigation of their newest member, who faces allegations that he sexually abused several teenagers when in his 30s.

“Decency wins,” tweeted Republican Senator Jeff Flake, of Arizona, who had written a check to Mr Jones’s campaign.

Even without the sexual misconduct allegations, there would have been plenty in Mr Moore’s background and fundamentalist Christian ideology for Democrats use in attacks against Republicans. And Mr Moore would have been a wildcard in Senate votes, owing no loyalty to the Republican leadership.

Still, losing the seat was hardly a victory for Republican leaders. Mr Jones diminishes the GOP’s already slim Senate majority to 51 seats, giving the Democrats a better chance of taking over the Senate in 2018, and making it harder to generate the votes they need to pass President Donald Trump’s legislative agenda.

And it is bad for Trump, who has now lost twice in Alabama, one of the reddest states in the country. He initially endorsed Senator Luther Strange, who was appointed to fill the seat left vacant when former senator Jeff Sessions became Mr Trump’s attorney general, but Mr Strange lost to Mr Moore in the summer’s Republican primaries.

Democrats also carried the governors’ races in Virginia and New Jersey in November, an outcome they claimed as a referendum on Mr Trump’s presidency.

In a tweet posted on Tuesday night, Mr Trump looked ahead to the next election, saying: “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!”

Mr Jones was only elected to complete Mr Sessions’s term, which expires in 2020.

Patrick Murray, director of the Monmouth University Polling Institute, said the win was part of a pattern where Democrats are turning out to take a stand against Mr Trump,

“This wouldn’t happen without Roy Moore’s personal behaviour on top of that,” Mr Murray said. “But it was Trump that provided the spark for Democrats to feel that they need to exert themselves in these off-year elections. And that, I think, is an indication for what we’re looking at for 2018.

“Democrats are starting to smell blood."

Mr Jones’s victory will not only give Democrats momentum heading into 2018, but it will embolden party members calling for an investigation into Mr Trump’s history of sexual harassment allegations, said Jim Manley, a former spokesman for former Senate Minority Leader Harry Reid, a Democrat.

“It’s going to highlight how much this issue of sexual harassment is resonating with voters now, especially women voters,” Mr Manley said. “Even in conservative Alabama, it tripped up a guy who by many accounts is a hero to conservatives. It gave Democrats an opening to try to take this guy down that they didn’t necessarily have.”

Democrats need to win two more Republican seats – and hold all of their own – to win the Senate majority in 2018. That will not come easy, given they are defending 10 seats in states Mr Trump won and other opportunities appear limited, at this point, to Arizona and Nevada. But the Alabama win puts it in the realm of possibility.

Mr Jones is a former US attorney who is known for prosecuting two Ku Klux Klansmen in the 16th Street Baptist Church bombing which killed four African-American girls.

Mr Moore, meanwhile, is a controversial figure who was twice removed from his position as chief justice of the state Supreme Court. He opposes LGBT rights and has spoken out against Muslims.

Senator Bob Corker, a Republican, said Mr Moore was already a "bridge too far" even before the sexual misconduct allegations.

His party colleague, Alabama’s senior senator, Richard Shelby, told CNN he cast a write-in vote for a Republican other than Mr Moore, adding: “The state of Alabama deserves better.”

In his victory speech, Jones said, “This entire race has been about dignity and respect. This campaign has been about the rule of law.”

But Mr Moore, meanwhile, refused to concede and raised the possibility of a recount during a brief appearance at a sombre campaign party in Montgomery.

"It's not over," Mr Moore said. "We know that God is still in control."