Mr. Strange has been locked in a close race with Representative Mo Brooks and Roy Moore, the controversial former state Supreme Court justice. There are also a number of other, lesser-known Republicans vying for the nomination. If nobody gets over 50 percent in the Aug. 15 vote, the party’s nominee will be decided in a September runoff.

Mr. Strange, who heard from the president about his plans earlier Tuesday, had been lobbying for weeks to win Mr. Trump’s support, singing the president’s praises in TV ads, online and through as many Fox News appearances he could schedule. Mr. Strange and his allies also have made Mr. Brooks’s attacks last year on Mr. Trump’s presidential campaign a centerpiece of the primary.

Reince Priebus, the former White House chief of staff, had been uneasy about Mr. Trump spending political capital in such a fluid campaign — especially with Mr. Brooks, a member of the House Freedom Caucus, also in the field.

But Mr. Trump threw caution to the wind with his Tuesday night tweet, dealing Mr. Brooks a devastating blow. The endorsement was also sure to delight Senator Mitch McConnell, Republican of Kentucky and the majority leader, for backing the 6-foot-9 lawmaker known to many as Big Luther.