BIRMINGHAM, Ala. — In a stunning conclusion to a contest that received international attention, Democrat Doug Jones is the projected winner over Republican Roy Moore in the Senate race in this deeply conservative state, according to an NBC News projection.

It took an extraordinary alignment of events, including a sex scandal involving teenagers, for Alabamians to elect their first Democrat to the Senate in 25 years, but they triggered a political earthquake that will be felt far and wide.

With 99 percent of the vote in, Jones was leading 50-48 percent, or 673,236 votes to 652,300 votes — a margin of more than 20,000.

However, Moore refused to concede Tuesday night, telling supporters in Montgomery that he may pursue a recount.

"It's not over," Moore said. "That's what we've got to do, is wait on God and have this process play out."

The current margin appears too large for an automatic recount, which is triggered under state law if the candidates are separated by less than half a percentage point, but Moore could call for a recount if he's willing to pay for one himself.

The projected outcome was another stinging defeat for President Donald Trump, who bucked his party's congressional leadership to stage a last-minute rescue mission for Moore. And it was his second in a state he won overwhelmingly in 2016, after he tried to pull Sen. Luther Strange across the line in the GOP primary against Moore in September.

Trump responded Tuesday night on Twitter with relative magnanimity, conceding the race before Moore spoke.

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

Early Wednesday, the president tweeted the reason why he originally backed Strange — because he knew Moore "will not be able to win the General Election." During a rally in September before the GOP primary, Trump told voters that "Roy has a very good chance of not winning in the general election."

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

During the race, Moore adamantly denied multiple allegations of sexual misconduct with underage girls decades ago, which had dogged his campaign for weeks and kept him largely hidden from voters down the homestretch. But the claims proved too difficult for him to overcome.

Stronger than expected turnout — especially from African-Americans — helped Jones overcome the state's conservative slant, echoing results Democrats have seen in other races this year.

With his voice hoarse after a long campaign that included more than 230 events over the past two months, Jones, 63, thanked supporters for believing in him against the odds as confetti rained down from the ceiling.

"I have been waiting all my life and now I just don’t know what the hell to say," the former federal prosecutor said. "This entire race has been about dignity and respect. This campaign has been about common courtesy and decency."

The unusual election, which proved almost impossible to poll, captivated both national political observers and voters in Alabama.

"This win is not only for Doug, or Alabama, or for America. The world was watching this, and we put on today," Randall Woodfinn, who was elected mayor of Birmingham in October, told NBC News.

Many of the long-suffering Alabama Democrats who gathered here to celebrate with Jones were stunned by the result, expressing disbelief at the political miracle they had just pulled off.

"I never, never, never thought this would happen," said Betsy McGuire, who volunteered for Jones even as most of her friends and neighbors in the suburb of Homewood stuck with Moore.

McGuire, who said she had never done anything political until Trump’s election, initially got involved to help Jones lose more gracefully — winning seemed out of the question — hoping to get to him to around 40 percent of the vote on Election Night. "Then at least Alabama would be in the game," she said.

Michael Griggs said the election broke the stranglehold, at least for a moment, of the small group of wealthy and mostly white power brokers he feels control the state. "For once, the money didn't win," he said.

But Birmingham City Councilwoman Sheila Tyson, who coordinated a state-wide effort focused on organizing black women, said she knew Jones would win two months ago when one woman said she would use her last $20 to buy gas to organize voters.

"Living in a state with so much racism ... people are tired of it," Tyson said.

Black women are consistently on the bottom rung of economic indicators, Tyson said, even though they run more households than other groups.

"We knew somebody had to carry him over the line," she said of Jones, "and we knew it was going to have to be us."

The final sprint of the campaign was marked by the appearance of celebrities and national political figures, who had previously stayed away from a state known for being hostile to outsiders.

Trump recorded a robocall for Moore, while former President Barack Obama and Vice President Joe Biden ran their own for Jones. Basketball star Charles Barkley stumped with Jones at an election eve rally on Monday and urged Alabamians to "stop looking like idiots" and reject Moore.

Former Trump adviser and Breitbart News leader Steve Bannon flew into the state for a third time Monday to reinforce that he had stuck with Moore when other Republicans turned their back on him, just as Bannon had defended Trump after the release of the "Access Hollywood" tape a month before the presidential election last year.