“This is what happens to someone who loyally gets appointed Attorney General of the United States & then doesn’t have the wisdom or courage to stare down & end the phony Russia Witch Hunt,” Mr. Trump tweeted. “Recuses himself on FIRST DAY in office, and the Mueller Scam begins!”

If Republicans do win the seat, it will provide them some breathing room in what is expected to be an otherwise difficult year to defend their 53-47 majority, with a polarizing president on the ticket and concern that Republican senators from Maine to Colorado are in jeopardy. With the vice president empowered to cast tiebreaking votes in the Senate, Democrats need a net gain of three seats to take the majority if the party captures the White House; four if not.

The primary in Alabama was a humbling experience for Mr. Sessions, who was treated as a castoff by the Republican Party he helped transform by championing a more nationalistic, anti-immigration, anti-free trade agenda years before Mr. Trump ran for office sounding those themes. Mr. Sessions’s predicament became a cautionary tale about the grudges and grievances that are at the heart of Mr. Trump’s politics. And it left little doubt about how successful Mr. Trump has been at turning his party into a vessel for his own political advancement, where the fundamental organizing principle is not conservative ideology or policy, but personal loyalty to him.

Mr. Sessions’s failure to make a stronger showing on Tuesday was a striking reversal of fortune for a politician whose standing in Alabama was once so formidable that the last time he ran for re-election in Alabama, the Democratic Party did not even bother to nominate a candidate to oppose him.

His problem this time was that not enough Republican voters appeared to recall the pre-Trump Jeff Sessions, a beloved figure on the right who helped fuel a populist backlash that antagonized Republican leaders over the very issues that are now at the heart of Mr. Trump’s nationalist agenda.

Instead, many of them saw the diminished attorney general who had been harangued, humiliated and belittled by Mr. Trump, who attacked Mr. Sessions as “scared stiff” and “Missing in Action” and told NBC News that appointing him was his “biggest mistake” he had made as president.

Mr. Trump’s low opinion of Mr. Sessions aside, the race was shaped by other factors that did not work in his favor. Mr. Tuberville, who emphasized his lack of experience in politics, called for Alabama to turn the page and elect fresh leadership. Mr. Sessions, who is 73, can no longer claim to be the renegade outsider he once was.