Doug Jones’ was the first Democratic Senate victory in a quarter-century in Alabama, one of the reddest of red states, and proved anew that party loyalty is anything but sure in the age of Mr Trump.
Republican Roy Moore’s loss was a major embarrassment for the president and a fresh wound for the nation’s already divided Republican Party.
“We have shown not just around the state of Alabama, but we have shown the country the way - that we can be unified,” Mr Jones declared as supporters in a Birmingham ballroom cheered, danced and cried tears of joy.
Still in shock, the Democrat struggled for words: “I think that I have been waiting all my life, and now I just don’t know what the hell to say.”
Mr Moore, meanwhile, refused to concede and raised the possibility of a recount during a brief appearance at a sombre campaign party in Montgomery.
“It’s not over,” Mr Moore said. He added: “We know that God is still in control.”
From the White House, Mr Trump tweeted his congratulations to Mr Jones “on a hard-fought victory” - but added pointedly: “The Republicans will have another shot at this seat in a very short period of time. It never ends!”
Mr Jones takes over the seat previously held by Attorney General Jeff Sessions and the term expires in January of 2021.
The victory by Mr Jones, a former US attorney best known for prosecuting two Ku Klux Klansmen responsible for Birmingham’s infamous 1963 church bombing, narrows the Republican Party’s advantage in the US Senate to 51-49.
That imperils already-uncertain Republican tax, budget and health proposals and injects tremendous energy into the Democratic Party’s early push to reclaim House and Senate majorities in 2018.
Still, many Washington Republicans viewed the defeat of Mr Moore as perhaps the best outcome for the party nationally despite the short-term sting.
The fiery Christian conservative’s positions have alienated women, racial minorities, gays and Muslims - in addition to the multiple allegations that he was guilty of sexual misconduct with teens, one only 14, when he was in his 30s.
“Tonight’s results are clear - the people of Alabama deemed Roy Moore unfit to serve in the US Senate,” said Colorado Senator Cory Gardner, who leads the national Republican Party’s Senate campaign arm and called on Mr Moore to quit the race weeks ago.
“Short-term pain, long-term gain,” former Minnesota Senator Norm Coleman, a Republican, tweeted. “Roy Moore and Steve Bannon losing tonight is big win for the GOP (Republican Party). ... Moore would have buried GOP in 2018.”
A number of Republicans declined to support him, including Alabama’s long-serving Senator Richard Shelby.
But Mr Trump lent his name and the national Republican Party’s resources to Mr Moore’s campaign in recent days.
Had Mr Moore won, the Republican Party would have been saddled with a colleague accused of sordid conduct as Republicans nationwide struggle with Mr Trump’s historically low popularity.
Senate leaders had promised that Mr Moore would have faced an immediate ethics investigation.
Mr Moore sidestepped questions about sexual misconduct as he arrived at his polling place on horseback earlier in the day.
Alabama state law calls for a recount if the margin of victory is less than one-half of one percentage point. With all precincts reporting, Jones led by 1.5 points - three times that margin.
If the secretary of state determines there were more write-in votes than the difference between Mr Jones and Mr Moore, the state’s counties would be required to tally those votes.
It is not clear how that would help Mr Moore, who ended the night trailing Jones by more than 20,000 votes.
Democrats were not supposed to have a chance in Alabama, one of the most Republican-leaning states in the nation. Mr Trump defeated Democrat Hillary Clinton here by nearly 28 points just 13 months ago.