In a stunning upset, Democratic candidate Doug Jones beat Republican Roy Moore Tuesday after an ugly special election for Alabama’s Senate seat that turned on women accusing the GOP nominee of preying on them when they were as young as 14.

The result is a setback for President Trump, who strongly endorsed Moore just ahead of the election after other Republicans tried to distance themselves over the accusations of sexual impropriety.

Jones, a former prosecutor, who in 1963 helped convict Ku Klux Klan members responsible for a bombing at a black church in Birmingham that killed four girls, managed to convince Alabama voters to elect a Democrat for the first time in more than 20 years.

"The people of Alabama have more in common than divide us," Jones said at his election night victory party. "We have shown — not just around state of Alabama -- we have shown the country the way that we can be unified."

Despite the celebrations in the Jones camp Tuesday night, Moore declared at his election night rally that he will not concede the contest and floated a possible recount.

"When the vote is this close, it is not over," he said.

Moore, who ran a race in the model of Trump’s presidential bid, gained support from the president’s former adviser Steve Bannon early on. Trump didn’t support the former judge, who’d been twice removed as Alabama’s chief justice for defying federal judges, in the Republican primary, but rallied to his side in the final days. Trump’s endorsement came after members of his own party called on Moore to drop out of the race over alleged sexual misconduct.

Still, Trump’s full-fledged support and a reinjection of cash from the Republican National Committee with nine days to go failed to push Moore across the finish line.

Jones pulled off the shocking defeat by appearing to convince the large number of voters who remained undecided until they went to the polls.

Jones had stayed away from attacking Moore on the allegations that he preyed on young girls until the final stretch. He launched TV ads that listed the names of Moore’s accusers and asked Alabama voters, "Will we make their abuser a U.S. senator?"

Alabama answered, and it was a no.

"This campaign has been about the rule of law. This campaign has been about common courtesy and decency," Jones said Tuesday night at his victory rally.

Moore’s loss left Republicans breathing a sigh of relief. Though a one-seat majority will cause headaches for Senate Majority Leader Mitch McConnell, making it that much harder to pass legislation without Democrats, the alternative appeared far worse for Republicans. In the hours leading up to polls closing, Republicans weren’t sure what they would do if Moore won. McConnell had said an ethics probe would be likely, but removing him would have been a difficult and potentially time-consuming proposition.

Jones' win boosts Democrats heading into 2018 and provides them a pathway, however steep, to win the majority in the Senate.

Sen. Chris Van Hollen, chairman of the Democratic Senatorial Campaign Committee, said Jones' victory was a clear rejection of "the new Republican Party of Roy Moore and Donald Trump."

"Today, in one of the most Republican states in the nation, the people of Alabama chose common decency and integrity over partisan politics," Van Hollen, D-Md., said in a statement. "Donald Trump and the Republican National Committee did the opposite, siding with a candidate who wanted to drag Alabama back to the days of George Wallace and faced a mountain of credible evidence that he had engaged in child sexual abuse."

Van Hollen added that Trump, Republican Senate candidates, and the RNC showed voters who they are and "we will make sure voters do not forget it.”

"Doug Jones will be an outstanding Senator who will represent Alabama well," said Senate Minority Leader Chuck Schumer D-N.Y.