Democrat Doug Jones speaks Tuesday, Dec. 12, 2017, in Birmingham, Ala. In a stunning victory aided by scandal, Jones won Alabama’s special Senate election, beating back history, an embattled Republican opponent and President Donald Trump, who urgently endorsed GOP rebel Roy Moore despite a litany of sexual misconduct allegations. (AP Photo/John Bazemore)

BIRMINGHAM, Ala. — Democrat Doug Jones has won the special election to fill a Senate seat in Alabama, according to exit polls and returns – a shocking upset in a solidly Republican state, in which massive turnout among African-American voters helped defeat a candidate enthusiastically backed by President Donad Trump.

The Associated Press called the race at 10:23 p.m. Eastern time. With about 98 percent of precincts reporting, Jones was leading by about 10,000 votes, with 49.5 percent of the vote as compared to Republican Roy Moore’s 48.8 percent.

It appeared that write-in candidates played a key role in the race: combined, they got 22,000 votes, more than the gap between the winner and the loser. Richard Shelby, R, Alabama’s other senator, had appeared to encourage the state’s Republicans to choose that option: Shelby said he wrote the name of an undisclosed “distinguished Republican” rather than vote for Moore.

Just after 11 p.m. Eastern time, Trump congratulated Jones in a Twitter message, saying “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.”

U.S. Senate candidate Roy Moore leaves the stage after speaking at the RSA activity center, Tuesday, Dec. 12, 2017, in Montgomery, Ala. Moore did not concede defeat to his Democratic opponent Doug Jones. (AP Photo/Mike Stewart)

Moore, however, refused to concede the race.

Speaking to supporters at about 11:34 p.m. Eastern time, Moore said he thought the race might go to a recount, which state law requires when a race is within 0.5 percentage points.”When the vote is this close . . . it’s not over,” Moore said.

Jones’ lead, at that point, however, still exceeded 0.5 percent.

Then, Moore – who had spent much of his campaign railing against the news media – asked that the press visit the Alabama secretary of state and ask how the recount provision might be triggered in this case – that “the press will go up there and talk to them to find out what the situation is.”

“What we’ve got to do is wait on God,” Moore said.

Jones would become the first Democrat elected to the Senate from Alabama since 1992. The Senate seat came open when Trump chose Jeff Sessions to become attorney general earlier this year.

Jones’ victory followed a pattern set earlier this year in Virginia’s gubernatorial election: a wave of enthusiasm among the Democratic Party’s traditional base, which was aided by a swing from Republicans to Democrats among well-educated suburban voters.

In all, 10 of Alabama’s 67 counties flipped from red to blue: they had supported Trump in 2016, but not voted for Jones. Among them: vote-rich Madison County, home to Huntsville – a highly educated city with huge defense and aerospace industries.

Jones, 63, is a former U.S. attorney best known for prosecuting two Ku Klux Klan members for the 1963 bombing of a Birmingham Baptist church, which killed four girls. The bombers were not tried until the 1990s.

He has never held elective office.

In Alabama, the night’s early returns showed Moore ahead, as mainly rural votes came in. But Jones surged ahead after 10 p.m. Eastern, as large cities like Mobile, Montgomery and Birmingham reported huge increases in turnout and large margins for the Democrat. Overall, news reports indicated that statewide turnout had smashed expectations, roughly doubling what officials had predicted.

At Jones’ election-night party in Birmingham, the crowd buzzed with nervous energy as the victory took shape.

His supporters watched CNN on a big screen, with Journey’s “Don’t Stop Believin'” revved up over the loudspeakers, when the race was called for Jones. The crowd erupted.

Supporters clapped, hugged each another and looked at the results on their phone, some in disbelief, some with a sense of validation and triumph.

“I just had a feeling!” one man exclaimed.

Paulette Roby, 67, said she had been worried that Moore would drag the state’s race relations back toward Alabama’s troubled past.

“We’ve got to have somebody that’s going to represent the state of Alabama with good faith,” said Roby, an African-American who said she was active in the civil rights movement. “We can’t go back. Why would we go back?”

Jones’ victory will reshape the calculus of the Senate, where Republicans will see their thin majority shrink from two votes to one.

With Jones’ win, Senate leaders will face pressure from Democrats to seat him before final votes on the GOP tax bill. The Alabama secretary of state’s office said the election result could be certified with the Senate as early as Dec. 27 to 29.

But Senate Majority Leader Mitch McConnell, R-Ky., said Tuesday, before the Alabama polls closed, that Strange will remain in the Senate through the end of the current session.

“Once the state certifies and sends us the paperwork, the new Senator is sworn in. But since the state said they don’t expect to certify until the end of the month, and we expect to finish before the end of the month…” McConnell spokesman Don Stewart wrote in an email.

While Republicans are still likely to pass a huge tax-reform push before Jones is officially seated, the rest of the GOP’s agenda may now plunge into jeopardy.

The coalition that backed Jones was sketched out in early exit polls. They indicated that black turnout might be slightly higher than the levels in 2012 and 2008, when Barack Obama was on the ballot. African-Americans made up 28 percent of the electorate in 2008, and 29 percent in 2012. In this election, they make up about 3 in 10 Alabama voters so far on Election Day according to preliminary exit polls.

“They’re coming out,” said Kim Payton, a city councilwoman in the town of Hayneville, Alabama – meaning black voters. Hayneville is located in the state’s “black belt” – rural area named for its dark soils, which also has a strong contingent of African-American voters.

By contrast, white evangelical voters seemed to make up a smaller percentage of the electorate this year than they had in past elections. That group made up just over 4 in 10 voters this year, compared with 47 percent of the state’s electorate in both 2012 and 2008 presidential elections, according to Alabama exit polls.

Moore, a former state chief justice who has taken strong stands on Christian morality and has said “God’s law” trumps the U.S. Constitution, was counting on the evangelical vote.

Other exit polls seemed to indicate that this electorate would be different than the one that came to the polls in November 2016. Trump, for instance, won Alabama with 62 percent of the vote in 2016. But preliminary exit polls Tuesday found that just under half of Alabamians approve of Trump’s performance, while about an identical share disapprove. About 4 in 10 voters say they “strongly disapprove” of Trump, compared with just over 3 in 10 who “strongly approve.”

Part of the reason was a scandal that hobbled Moore – the judge, who’d built his career as an outspoken voice for Christian morals, had reportedly pursued romantic relationships with teenagers while he was in his 30s. Moore has denied any wrongdoing.

One voter in Hayneville, 49 year-old masonry instructor, Robert Holcombe, said the allegations against Moore particularly bothered him. “We took Roy Moore to be a church man, an upright man,” Holcombe said. “I have an 18-year-old daughter. I know I wouldn’t want him messing with her at age 14.”

During the campaign, Jones sold himself as a centrist who would work with Republicans – and as a politician who would not embarrass Alabama or drive away business. He took advantage of a scandal that began with a report in The Washington Post: several women said that Moore had pursued romantic relationships with them decades before, when he was in his 30s and they were in their teens. Moore has denied wrongdoing.

“This election is going to be one of the most significant in our state’s history in a long time,” Jones said during an election-eve rally in Birmingham. “And we’ve got to make sure that at this crossroads in Alabama’s history, we take the right road.”

Jones also encouraged voters to put “decency” ahead of party loyalty and urged them to consider how Alabama will be viewed by business leaders as a result of the election.

In the short term, Jones’ victory will further narrow the GOP’s thin margin of control in the Senate, leaving Republicans with a 51-49 majority. Already, Republicans have struggled to pass legislation on health care and taxes, since only a few GOP defectors can sink any measure.

In the longer term, Jones win may signal the limits of President Trump’s political pull – and the drawbacks of remaking the GOP in his image.

Trump has now seen two of his candidates lose in Alabama, a state that had been at the heart of his support in last year’s election. First, Trump supported Sen. Luther Strange, the Republican state attorney general who was appointed to fill Session’s seat. But Strange lost to Moore in the GOP primary runoff.

Then, in the general election, Trump backed Moore.

After the allegations arose against him, Moore – who was advised by Trump’s former chief strategist Steve Bannon – used the playbook that Trump used after the “Access Hollywood” videotape was revealed last year. In Moore’s case, the candidate attacked his accusers as liars, and cast their stories as part of a conspiracy by the news media and the political establishment.

After that, Senate GOP leaders had called on Moore to quit. He didn’t. Instead, Moore relied on the help of Bannon. Moore criticized his accusers, denied all the reports of improper behavior, and blamed a conspiracy of the news media and establishment politicians.

After at first being cautious with his support, Trump backed Moore enthusiastically as the election drew near. He become one of Moore’s most fervent defender in recent days, tweeting about him repeatedly and recording a robo-call to drive out the vote. “Roy Moore will always vote with us. VOTE ROY MOORE!” Trump tweeted on Election Day.

The National Republican Senate Committee, which normally supports GOP candidates, never got back into the race on Moore’s behalf. After his defeat, NRSC chairman Cory Gardner, R-Colo., issued an unusually dismissive statement, saying “Tonight’s results are clear – the people of Alabama deemed Roy Moore unfit to serve in the U.S. Senate”

In interviews with voters on Tuesday, Trump’s influence seemed to be slight. For many voters, it was Moore that defined the race – both for those who supported him, and those who supported Jones.

“This is the first time I’ve voted for a Democrat,” said Henry Waller, 24, who works in logistics for a granite company, said at a polling place in Mountain Brook, Alabama. “I’m a Christian, and I think Moore represents the absolute worst way to put Christianity into politics.”

Sessions, whose departure from the Senate prompted Tuesday’s election, said Tuesday that he voted but did not say it was for Moore.

“I voted absentee,” Sessions said in Baltimore, responding to a question from a reporter. “I value the sanctity of the ballot. The people of Alabama are good and decent. They’ll make right decision.”

Fahrenthold reported from Washington. Sullivan and Weigel reported from Alabama. Elise Viebeck, Michael Scherer, Philip Rucker and Scott Clement in Washington and Larry Bleiberg and Jenna Johnson in Alabama contributed to this report.