Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

MONTGOMERY, Ala. — Conservative firebrand Roy Moore ousted incumbent Sen. Luther Strange on Tuesday night in the heated Alabama Senate race, handing a defeat to President Donald Trump, who had endorsed and campaigned for Strange.

Moore's victory over Strange was a landslide — 54.6 percent (262,204 votes) to 45.4 percent (218,066 votes), with 100 percent of the vote counted — despite Trump and Senate Majority Leader Mitch McConnell having taken extraordinary measures and spending millions of dollars trying to knock back the twice-removed former chief justice of the Alabama Supreme Court.

A cheer went up at Moore's victory party here as the race was called, and Strange wasted no time issuing a statement congratulating his opponent. "May God be with him," Strange said.

Trump and Vice President Mike Pence — who has campaigned for Strange in Alabama just 24 hours earlier — moved quickly to align themselves with Moore.

Congratulations to Roy Moore on his Republican Primary win in Alabama. Luther Strange started way back & ran a good race. Roy, WIN in Dec! — Donald J. Trump (@realDonaldTrump) September 27, 2017

Spoke to Roy Moore of Alabama last night for the first time. Sounds like a really great guy who ran a fantastic race. He will help to #MAGA! — Donald J. Trump (@realDonaldTrump) September 27, 2017

And Moore professed his backing for Trump. "Don't let anybody in the press think that because he supported my opponent, I do not support him," Moore said in his victory speech.

Still, the outcome is an embarrassment to Trump, whose coattails proved shorter than bargained for in a state he overwhelmingly carried in last year's presidential election. And it may reignite the feud between Trump and McConnell if either is tempted to blame the other for the loss.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

Trump made his first major electoral intervention as president at McConnell’s behest, recording robocalls, holding a rally in Huntsville and offering repeated support for Strange on Twitter as recently as Tuesday morning.

Strange showcased Trump's backing as the central pillar of his campaign in its closing days, invoking it repeatedly in the race's only debate last week his TV ads.

Muddying the waters was the fact that many of Trump's own former aides and surrogates — including one of his Cabinet secretaries, Ben Carson at HUD — lined up against him to support Moore. Former chief strategist Steve Bannon turned Breitbart News, the outlet he runs, into a nonstop anti-Strange attack machine, and used an election eve rally to warn the GOP establishment, "Your day of reckoning is coming.”

Bannon took a victory lap while introducing the candidate Tuesday night after his win, telling supporters they were "starting a revolution with Judge Moore’s victory."

Cygnal, a GOP consulting firm based in Alabama, commissioned a poll and found that "Trump’s endorsement does not appear to have impacted the race," the firm's Matt Hubbard wrote in a memo shared with NBC News.

Most voters polled said they were not influenced by Trump, and those that did were equally likely to say it pushed them toward Moore — perhaps because of Trump off-hand comment at the Huntsville rally last Friday to back Strange that he "might have made a mistake" in supporting him.

Still, comments like that let Trump hedge his bets considerably and convinced Moore supporters that he only backed Strange under duress from McConnell.

Moore, whom Sarah Palin endorsed and dubbed "deplorable before it was cool to be deplorable," is a proudly uncompromising religious fundamentalist who has vowed to wage war on the Washington establishment from the inside.

That divisiveness gives Democrat Doug Jones, a former U.S. attorney, an outside shot at winning the Dec. 12 general election for Attorney General Jeff Sessions' old Senate seat, analysts said.

Republican candidate for the U.S. Senate in Alabama, Roy Moore, speaks to reporters after declaring victory in the party primary on Tuesday in Montgomery. Scott Olson / Getty Images

Trump himself predicted Moore is "going to have a very hard time getting elected against the Democrat," while Strange said his opponent would be "an anchor around the neck of the party."

Jones responded to the results in the GOP contest by suggesting Moore would "embarrass the people of Alabama" if elected to the Senate.

Pro-Strange forces outspent Moore nearly 5-to-1 on television advertising, according to data provided to NBC News by Advertising Analytics LLC. And Trump’s endorsement put Strange on the national radar and the front page of every newspaper in the state and at the top of almost every broadcast.

But in an election in which fewer than three in 20 voters were expected to turn out, according to the secretary of state, the anti-establishment mood and Moore’s enthusiastic base, including the evangelical community, trumped Trump’s endorsement.

"Roy Moore, at least to a very large minority of the Alabama population, is an absolute folk hero," said Quin Hillyer, a conservative commentator and former Alabama congressional candidate.

For McConnell, the day will likely go down as one of the worst of his tenure, with the demise of a final bill to repeal the Affordable Care Act followed by the defeat of an incumbent his super PAC spent almost $10 million supporting.

"He ran a spirited campaign centered around a dissatisfaction with the progress made in Washington. I share that frustration," McConnell said.

That group, the Senate Leadership Fund, quickly pledged to help Moore keep the seat in GOP hands.