In a stunning setback for the Republican Party, Democrat Doug Jones was elected Alabama's next senator Tuesday, flipping a deeply red state after a campaign that showcased the increasing power of sexual misconduct allegations and the limits of President Trump's political influence.

Jones's victory in a part of the Deep South that has not elected a Democratic senator since 1992 was a dramatic repudiation of his opponent, Roy Moore, a former state judge twice removed from office. Moore responded to allegations that he made sexual advances on teenagers when he was in his 30s by describing his campaign as a "spiritual battle" against a conspiracy of Republican and Democratic leaders in Washington.

"I was on the Moore bandwagon at first, years back, before you really knew what was going on with him," said Scott Medders, 32, who voted for Jones and then attended a watch party in Gadsden, Ala., Moore's home town. "When Jones gets there, I hope he strengthens the military and helps with the wall. But I could not vote for Moore."

After the race was called by the Associated Press, Moore declined to concede defeat, saying he believed that the margin of victory could narrow enough to trigger an automatic recount. "Realize that when the vote is this close that it's not over," he said. "We also know that God is always in control."

The Alabama Republican Party said it would not support Moore's push for a recount.

Secretary of State John Merrill said after Moore spoke that even though the margin of victory stood at more than 1 percent, an automatic recount could still be ordered if a review of write-in votes and military ballots narrowed the margin of victory to less than 0.5 percent.

1 of 76 Full Screen Autoplay Close Skip Ad × Doug Jones defeats Roy Moore for the U.S. Senate seat from Alabama View Photos Republican Roy Moore, who has been accused of sexual misconduct, faced off against Democrat Doug Jones in a special election. Caption Republican Roy Moore, who has been accused of sexual misconduct, faced off against Democrat Doug Jones in a special election. Jabin Botsford Buy Photo Wait 1 second to continue.

Jones's victory portended the head winds facing Republicans in the 2018 midterm elections, coming just a month after a historic Republican wipeout in the battleground state of Virginia. With Jones in office, Democrats will have a credible, if still difficult, path to retake control of the Senate two years into Trump's term.

The result could also become a factor in upcoming legislative battles, as Republicans will have one less vote in the narrowly divided Senate in 2018. Although Senate Majority Leader Mitch McConnell (R-Ky.) — who opposed Moore's candidacy after the allegations emerged — has said that the GOP tax overhaul will be completed before the end of the year, when Jones is sworn into office, the impact of Tuesday's outcome on the ongoing debate is unknown.

Merrill's office said Tuesday that the election will be certified between Dec. 27 and Jan. 3, giving Republicans as little as two weeks to pass a federal budget and the tax legislation with their current 52-to-48 majority.

Trump tweeted his congratulations to Jones for his victory. "The write-in votes played a very big factor, but a win is a win," Trump wrote. "The people of Alabama are great, and the Republicans will have another shot at this seat in a very short period of time."

Trump won Alabama with 62 percent of the vote in 2016. He attempted to lead a late rally for Moore in the closing weeks of the election, recording a robo-call, hosting a rally in Florida near the state line and repeatedly warning Republicans to avoid electing a Democrat.

The president's former chief strategist, Stephen K. Bannon, all but adopted Moore as the public face of his insurgent effort to topple the congressional leadership of the Republican Party. Bannon appeared at both of Moore's rallies in the final week, and he deployed the full force of his Breitbart News operation to support the campaign.

[Bannon’s ‘season of war’ on GOP establishment may backfire with first battle]

It did not work. In the end, Jones won about 50 percent of the vote compared with about 49 percent for Moore, with Jones benefiting from strong African American turnout and a white share of the vote about twice as large as Barack Obama won in 2008. Fifty-six percent of women voted for Jones, according to exit polls, while 58 percent of men voted for Moore. Just under 2 percent of voters in the state wrote in a third candidate.

"Bannon's war on the GOP backfired, ricocheted and hit the president," said Scott Reed, a Republican political strategist at the U.S. Chamber of Commerce, who opposed Moore in the primary.

Exit polls showed a steep drop in support for Trump since his victory in 2016. Just 48 percent of voters approved of the president's job performance, higher than the national average but well below the levels of 2016, when Trump adopted Alabama as one of his favorite locations for large rallies. It was the second time in two months that the state flouted Trump's endorsement. Republican primary voters also rejected Sen. Luther Strange, the president's choice in the September runoff.

Jones, a former federal prosecutor who made his mark convicting Ku Klux Klan members for the 1963 bombing of the 16th Street Baptist Church, cast his campaign as an opportunity for the state to turn the page on the divisive politics of its past. He supported protecting entitlements such as Medicare and Medicaid, defended Obamacare and said he broadly supported abortion. A gun owner, he supported strengthening the background-check system.

"At the end of the day, this entire race has been about dignity and respect," Jones said at his victory rally, a raucous celebration in Birmingham. "This campaign has been about the rule of law. This campaign has been about common courtesy and decency and making sure that everyone in this state, regardless of which Zip code you live in, is going to get a fair shake."

At a Democratic watch party in Gadsden, where several of Moore's accusers lived, women who knew the accusers broke down in tears when the result was called. "There was a movement, the resistance movement, the day Donald Trump was sworn in," said Ann Green, the chair of the Etowah County Democratic Party. "It didn't just happen on the coasts. It happened in Alabama."

Democrats were aided by senior congressional Republicans who dropped their endorsements of Moore after the allegations of misconduct surfaced, including hard-line conservatives such as Sen. Mike Lee (Utah) and Sen. Ted Cruz (Tex.). McConnell had promised to open an ethics investigation if Moore won.

The Republican National Committee also pulled out of the race after the allegations surfaced, with Trump's initial blessing, but then reengaged in the final week of the campaign at the president's direction.

Six women told The Washington Post that Moore pursued them in the late 1970s and early 1980s. Five were teenagers at the time, and one was 22. Moore, who denied misconduct but admitted to possibly dating high school girls, was in his early 30s at the time. One woman, Leigh Corfman, said she was 14 and Moore was 32 when he took her to his house, gave her alcohol and touched her sexually.

Exit polls showed that voters were divided on the credibility of the accusations, with 51 percent saying they were definitely or probably true, compared with 44 percent who said they were definitely or probably false.

Alabama's senior senator, Richard C. Shelby, a Republican, announced that he could not support Moore, and the former holder of the seat, Attorney General Jeff Sessions, declined to make his vote public.

The outcome could have a major impact on Senate primaries in Arizona and Nevada, where Bannon and conservative activists are pushing insurgent candidates who establishment Republicans also fear will be unelectable statewide. These strategists will now step up their argument that candidate quality matters.

"I'm remembering Missouri and Indiana in 2012 — two can't-lose states where we nominated crap candidates and lost," said Steven J. Law, president of the Senate Leadership Fund, a group affiliated with McConnell that opposed Moore in the primary.

Bannon's allies struck back, blaming McConnell's lack of support for handing the seat to a Democrat. "Mitch McConnell and the Republican establishment got what they wanted tonight in Alabama," said Andy Surabian, a former Trump White House political aide who works with Bannon. "They handed this seat over to a liberal Democrat."

Jones was aided by a massive infusion of late fundraising, which allowed his campaign and a supportive outside group to dominate television, radio, direct mail and digital ad spending. Highway 31, a super PAC supporting Jones, spent $4.1 million in the final weeks of the campaign, compared with about $1.3 million from two outside groups backing Moore — most of which came from America First Action, a group that supports Trump's agenda.

The Jones campaign also outraised the Moore campaign by 5 to 1 in the general election, bringing in more than $10 million in total, as liberal donors around the country grew excited about a possible upset.

Jones also highlighted the ground game that his campaign ran in the lead-up to the race, citing 1 million phone calls, more than 200,000 doors knocked on and thousands of volunteers who braved early Alabama snow to participate in last-minute get-out-the-vote efforts.

Joe Reed, the chairman of the black Alabama Democratic Conference, said worries about low black turnout had been refuted by what he had seen on the ground. Operatives turning out votes in African American areas had coordinated with the Jones campaign, something less-well-funded Democrats had struggled to make happen.

"It's a high turnout," Reed said. "The Jones campaign gave us everything we asked them for."

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