He thanked Mr. Trump effusively, praising the president as a “loyal friend” and attempting to absolve him of any blame for the result. “If this causes him any trouble,” Mr. Strange said, “it’s not his fault.”

Mr. Strange’s defeat was the first time an incumbent senator with active White House support has lost since 2010, when Arlen Specter, the longtime senator of Pennsylvania, was beaten in a Democratic primary after switching parties.

But his loss was not just a blow to Mr. Trump. Mr. Moore relentlessly linked the senator to Mr. McConnell, who has made a priority of protecting his caucus from intraparty challenges, but is an increasingly polarizing figure among grass-roots Republicans. Despite the money and staff he directed to the race, Mr. McConnell became as much a liability as he was an asset, leaving Republicans nervously wondering what that may portend in other primaries next year.

On Tuesday night, Mr. McConnell said in a statement that he understood Mr. Moore had channeled “a dissatisfaction with the progress made in Washington.” Saying that he shared that frustration, Mr. McConnell said he was determined to help Mr. Moore win, and made no references to the bitter attacks on his leadership by Mr. Moore and his allies.

Mr. McConnell and his allies were jolted with another reminder of their limited control earlier in the day, when Senator Bob Corker of Tennessee, a popular incumbent, announced he would not run for re-election. As the first senator to opt out of seeking another term in 2018, Mr. Corker opened the way for another rowdy Southern primary in which the national party’s influence may be sorely tested.

Stephen K. Bannon, an ousted White House adviser and an enthusiastic backer of Mr. Moore, crowed about Mr. Corker’s retirement in brief remarks before Mr. Moore took the stage on Tuesday night. Mr. Bannon predicted a populist “revolution” would follow the Alabama results.

Mr. Strange’s demise was in some respects as much a local phenomenon as a national one, stemming from his appointment this year by then-Gov. Robert Bentley to fill the seat vacated by Attorney General Jeff Sessions. Mr. Strange, the state’s attorney general at the time, was overseeing an investigation into Mr. Bentley’s personal relationship with a close aide, suggesting to many in a scandal-weary state that there may have been a corrupt bargain. The newly appointed senator denied any wrongdoing, but never fully confronted the issue in a way that would eliminate the lingering cloud over the appointment.