The Democrats have snatched a formerly safe Republican Senate seat after the GOP splintered over its own nominee, an anti-gay, anti-Muslim former judge accused of molesting underage girls.
After trailing much of the night, Democrat candidate Doug Jones edged narrowly ahead of Republican Roy Moore in the homestretch, taking the race by less than two percentage point in a deep-red state where the GOP typically sweeps to electoral victory by huge margins.
With 100 per cent of polls reporting, the Democrat had 49.9 per cent of the vote, just ahead of Republican Roy Moore, with 48.4 per cent; write-in ballots accounted for the remainder.
Mr. Jones's triumph in the Alabama special election – held to fill the seat vacated by Jeff Sessions when President Donald Trump appointed him attorney-general earlier this year – gives his party a much-needed shot in the arm as it struggles to come back from losing the presidency last year.
The Democrats will be hoping the victory represents a serious blow against the populist current that carried Mr. Trump to office and was carried even further by Roy Moore, the GOP nominee who went down to defeat. It comes a little more than a month after the Democrats held onto the governorship of Virginia against a GOP campaign that tried to replicate the President's winning formula by stoking fear of immigrants.
For the Republicans, the result solves one problem while creating another. It cuts the GOP's majority in the Senate to 51, meaning just two Republican senators can hold up the party's legislative agenda. However, removing Mr. Moore from the equation gives the GOP leadership some breathing room as it tries to bridge divides within the party between the establishment and populist insurgents.
A former prosecutor best known for jailing two Klansmen who blew up a church and killed four African-American girls, Mr. Jones walked a fine line during the campaign between appealing to the Democrat base with promises to fight for better health care and education, and attempts to win over moderate Republicans horrified by Mr. Moore.
"I have always believed that the people of Alabama have more in common than divides us," Mr. Jones told supporters at his campaign victory rally. "We have shown the country the way that we can be."
A defiant Mr. Moore took the stage around 10.30 p.m. and refused to concede.
"When the vote is this close, it's not over," he said to cheers from his supporters. "We also know that God is always in control."
His campaign said military ballots had not yet come in, and under Alabama law there would be a mandatory recount if the final result was within half a percentage point. With 100 per cent of precincts reporting, Mr. Jones held a 1.5 per cent lead.
Mr. Trump, meanwhile, had already conceded the election with a congratulatory tweet to Mr. Jones: "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!"
On Wednesday morning, the President pointed to Mr. Moore's loss as the reason he backed establishment candidate Luther Strange in the GOP primary.
"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!" he tweeted.
A funereal atmosphere descended over the wood-paneled, high-ceilinged hall near the state capitol where Mr. Moore's supporters had gathered. Many bowed their heads in prayer as the New York Times website, projected on a screen at the front of the hall, flashed news of Mr. Jones's victory. Some raised their arms in the air and sang religious hymns. It was a contrast to earlier in the night when Mr. Moore was confident enough of victory to enter the hall to cheers and applause from an electric crowd.
For the result to mean anything for the Democrats, however, the party must prove it was the start of a larger trend rather than a one-off based on revulsion over Mr. Moore.
Mr. Moore won the Republican nomination by knocking off Mr. Strange on the back of an insurgent campaign reminiscent of Mr. Trump's own.
But Mr. Moore went even further than Mr. Trump in trying to translate the U.S.'s culture wars into political success.
He was twice kicked out of office as Alabama's chief justice for disregarding the separation of church and state: Once for installing a Ten Commandments monument in the courthouse and refusing to remove it, and again for ordering the state's judges to disregard the Supreme Court ruling legalizing same-sex marriage.
Over the years, Mr. Moore has said homosexuality should be outlawed, Muslims should not be allowed to sit in Congress and compared the Koran to Mein Kampf. Days before the election, he mused that America was last "great" during the era of slavery.
And during the campaign, seven women accused him of pursuing them when they were adolescents and he was a thirtysomething prosecutor in the 1970s. Mr. Moore denied having ever met the women and suggested there was a conspiracy between them, the media and the Democrats.
Mr. Moore's views found a receptive constituency with some Republicans.
"He's right about same-sex marriage being totally unnatural," said Jerry Ogles, 74, a minister, outside a barn rally for Mr. Moore the night before the election. "We weren't developed in this nation as a koranic nation, we were developed as a biblical nation. The founding fathers wrote the constitution … from a godly point of view."
But it also motivated a massive Democratic mobilization inside the state and nationally, with a parade of national figures showing up to stump for Mr. Jones and Alabamians insisting that they would not allow Mr. Moore's views to perpetuate retrograde stereotypes about the state.
"This right-wing extremism is not my experience of Alabama," said Sam Parkes, a 51-year-old pastor, as he picketed Mr. Moore's Monday rally. "The Alabama we know is where we were brought up to help people and be kind to one another."
And many Republicans – including supporters of Mr. Trump – found Mr. Moore's politics a bridge too far.
Liz Pittman, a 55-year-old GOP loyalist, said she hoped Mr. Moore would be promptly impeached if he won.
"If Roy Moore gets in, the best thing that could happen is if he gets knocked down," she said as she sat in the audience of a rally for Mr. Jones on Saturday in Birmingham. "We want a do-over."