Democrat Doug Jones has won the Alabama Senate special election, a victory that was a stunning upset in a deeply red state that voted overwhelmingly for President Trump. The president, who had backed Republican Roy Moore despite multiple accusations of sexual misconduct and assault, congratulated Jones on Twitter.

The win by Jones, projected by The Associated Press two hours after the polls closed Tuesday night, is sure to send shock waves through Washington. The special election to replace Attorney General Jeff Sessions, who left the Senate in February, was upended in November as multiple women came forward to say Moore had pursued them romantically as teenagers when he was in his 30s. Some alleged he had sexually assaulted them, including one woman who said he had initiated sexual contact with her when she was just 14. Moore has denied the accusations.

The unfolding controversy made what should have been a safe GOP race anything but. It's the first Democratic Senate victory in the state in 25 years and now gives Republicans an even narrower, 51-49 Senate majority that could imperil GOP legislative priorities in 2018. Jones will be up for re-election to a full term in 2020.

"Alabama has been at a crossroads. We have been at crossroads in the past, and unfortunately we have usually taken the wrong fork," Jones said in his victory speech. "Tonight, ladies and gentlemen, you took the right fork."

NBA legend Charles Barkley returned to his native Alabama to campaign for Jones in the closing days and told CNN after the Democrat's victory, "We got a bunch of rednecks and ignorant people, but we got some amazing people here and they rose up today."

It's the first major electoral blow to Trump since his own upset victory just over a year ago, giving Democrats a special election win after several near misses. Democrats were victorious in November in the Virginia and New Jersey governors' races, but the win in such a red state that Trump won by almost 30 points is a political earthquake heading into the 2018 midterm elections.

The president had come to Moore's defense, casting doubt on the women's allegations — much as he has done with the multiple women who have accused him of sexual assault. While Trump didn't campaign with Moore, he did hold a rally just across the border from Alabama in Pensacola, Fla., on Friday evening and recorded a robocall on his behalf, urging voters to choose Moore because he would support his agenda in the Senate. And after Trump reaffirmed his endorsement for Moore last week, the Republican National Committee reinstated its financial support for the GOP nominee after having pulled it following the accusations.

Trump was unusually restrained on Twitter late Tuesday, congratulating Jones and refraining from placing blame.

Moore refused to concede Tuesday night, saying that his campaign would "wait on God and let this process play out." He pointed to the uncertain write-in totals he believed could still change the outcome and trigger a recount. However, there has to be a difference of 0.5 percent or less between the two candidates to trigger an automatic recount, and with almost all the votes in, Jones' margin of victory was about three times that.

"The problem with this campaign is we've been painted in an unfavorable and unfaithful light," Moore said to a dwindling group of supporters Tuesday night.

Alabama Republican Secretary of State John Merrill said on CNN that the race will be certified between Dec. 26 and Jan. 3, and he cast doubt on the idea that the margin could change substantially even after write-in votes are totaled and validated.

Democrats were quick to point to the reversal by the RNC to renew support for Mooreand indicated they would use it as fodder heading into the 2018 campaign.

"Today, in one of the most Republican states in the nation, the people of Alabama chose common decency and integrity over partisan politics," Democratic Senatorial Campaign Committee Chairman Chris 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. ... President Trump, Republican Senate candidates and the Republican National Committee showed us exactly who they are by standing with Roy Moore — and we will make sure voters do not forget it."

Other national Republicans had been far less hospitable. Senate Majority Leader Mitch McConnell said he believes Moore's accusers and called on him to step aside, though he had softened his stance in recent weeks by saying the choice was up to Alabama voters. A Senate ethics investigation into the sexual assault allegations against Moore likely also would have followed had he won.

McConnell's allied superPAC, the Senate Leadership Fund, had spent heavily to support appointed Sen. Luther Strange in the primary, fearful that a Moore win would put the seat in jeopardy even before the sexual assault allegations against him surfaced.

Trump had backed Strange then, too, but Steve Bannon — his former chief strategist who has since returned to lead Breitbart News — supported Moore and rallied with him in the closing days.Bannon had long touted Moore as part of the "war" he had promised to wage on the GOP establishment, backing challengers to sitting incumbents who would take on McConnell and support Trump's agenda. But other national strategists warned Bannon's civil war would cost them winnable seats — something that came to fruition on Tuesday night.

"This is a brutal reminder that candidate quality matters regardless of where you are running," Senate Leadership Fund President Steven Law said in a statement. "Not only did Steve Bannon cost us a critical Senate seat in one of the most Republican states in the country, but he also dragged the President of the United States into his fiasco."

National Republican Senatorial Committee Chairman Cory Gardner, R-Colo., who is in charge of protecting the GOP's Senate majority in 2018, had withdrawn funding from Moore's campaign and at one point said he should be expelled from the Senate if he won.

"Tonight's results are clear — the people of Alabama deemed Roy Moore unfit to serve in the U.S. Senate," Gardner said in a statement soon after Jones was declared the winner. "I hope Senator-elect Doug Jones will do the right thing and truly represent Alabama by choosing to vote with the Senate Republican Majority."

Even the state's senior GOP senator, Richard Shelby, had admitted he didn't vote for Moore, saying "the Republican Party can do better" and revealing he had instead written in another candidate. That may have pushed other on-the-fence Republicans to also write in a candidate, and that margin could have helped ultimately tip the race in Jones' favor.

Condoleezza Rice, a Birmingham native who served as secretary of state under President George W. Bush, cut a robocall in the race, not-so-subtly urging voters in her home state to "reject bigotry, sexism and intolerance."

In the end, Republicans recognize that they may be better off without having to deal with Moore in their caucus.

"It may well be the best possible outcome. We don't have to apologize for Moore and Jones will win with less than 50% of the vote and will quickly fade into 2020," Barry Bennett, a former Trump campaign adviser, told NPR. "This race wasn't about anybody other than Roy Moore and his past. To the extent there were bullets fired, they were by Roy Moore into his own feet."

Other Trump allies blamed McConnell for not working to help Moore. A headline on Breitbart News proclaimed that "REPUBLICAN SABOTEURS FLIP SEAT TO DEMS."

"Trump is surrounded by strangers and political ignoramuses. They embarrassed the president tonight," said former Trump adviser Sam Nunberg, blaming Trump's political advisers — including daughter Ivanka and son-in-law, Jared Kushner — for leading him astray in endorsing Strange in the first place and later waffling on Moore.

Jones is a former U.S. attorney who is best known for prosecuting KKK members decades later for the killing of four young African-American girls in a 1963 Birmingham church bombing — a background that likely helped him spur a heavy black turnout that was critical for the Democrat to win.

According to exit polls, African-American turnout reached almost 30 percent — comparable to what it was for Barack Obama in 2008.

Over half of voters also said that the allegations against Moore were true, and 60 percent said the accusations were a factor in their vote.

Jones also ran a far more visible race than Moore in the final stretch. He had outspent Moore almost 10-1 and had an active campaign schedule, while Moore had been largely absent from the campaign trail in the final stretch — including leaving the state over the weekend to attend the Army-Navy football game in Philadelphia. Jones also had an active field operation, while the GOP nominee's staff was a skeleton crew.

Moore had remained defiant, however, using a very Trumpian strategy of running against the media and the D.C. establishment he says have conspired against him and are behind the allegations. And he won the GOP primary over the appointed Strange earlier this year, despite being heavily outspent by both Strange and the Senate Leadership Fund.

But even before the accusations of sexual assault surfaced, Moore was a controversial figure in Alabama politics who narrowly won election in the past and lost nominations for governor. He is a former Alabama Supreme Court chief justice who was twice removed from the bench, the first time for refusing to remove a statue of the Ten Commandments he'd had erected in the state judiciary building. Later, he was re-elected to the court but then suspended after he directed state judges to ignore the U.S. Supreme Court's 2015 ruling that legalized same-sex marriage.

Moore's Christian nationalist positions are something that he hoped wouldresonate with many of the state's white evangelical voters, and he neverbacked off his controversial positions against same-sex marriage and transgender rights. He had hammered home his opposition to abortion rights, in contrast to Jones. Ultimately, however, it wouldn't be enough for Moore to overcome the damaging accusations against him.

NPR White House Correspondent Tamara Keith contributed to this report.