Representative Martha Roby of Alabama prevailed on Tuesday in a Republican primary election that unfolded as a test of fealty to President Trump, defeating a challenger who assailed her for withdrawing her support for Mr. Trump in the last days of the 2016 campaign.
Her criticism of Mr. Trump cost Ms. Roby, a mainstream conservative seeking a fifth term, a clear-cut victory in an initial round of voting last month. She fell short of a majority, forcing her to compete in a runoff election against Bobby Bright, a populist former Democrat who served in Congress and as mayor of Montgomery, Alabama’s capital.
She handily held off Mr. Bright, The Associated Press reported, after overcoming suspicion about her loyalty to the president with help from an unlikely ally — Mr. Trump himself.
Unlike other Republicans whom Mr. Trump has gleefully helped push from office, Ms. Roby was not an eager antagonist during the 2016 presidential election, or since. Though she pronounced Mr. Trump “unacceptable” after the release of the “Access Hollywood” recording that showed him bragging about groping women, she has been an unflagging supporter since his inauguration.
So last month Mr. Trump, who easily carried Ms. Roby’s predominantly rural district in 2016 and remains popular there, extended to her a kind of political clemency that he rarely grants critics on the right. He endorsed Ms. Roby on Twitter, calling her a “consistent and reliable vote for our Make America Great Again Agenda.”
Perhaps just as importantly, Mr. Trump branded Ms. Roby’s challenger as wholly unacceptable. Alluding to Mr. Bright’s tenure in the House before switching parties, Mr. Trump called him a “Nancy Pelosi voting Democrat” — a label Ms. Roby and her allies consistently affixed to Mr. Bright during the campaign.
Ms. Roby wasted little time in expressing gratitude to the White House for its support.
“Sincere thanks to President Trump and Vice President Pence,” she told supporters at a victory celebration, according to the A.P. “I am so humbled that the people of Alabama’s Second Congressional District have again placed their trust and their confidence in me.”
She is now heavily favored to win the general election in the district, a largely rural and conservative seat that also includes Montgomery. The Democratic nominee is Tabitha Isner, a minister and business analyst.
The survival of Ms. Roby, 41, is a triumph for both Mr. Trump and the Republican establishment, and it is a testament to the hand-in-glove cooperation they have recently maintained on campaign matters, even as a range of policy disagreements on issues like tariffs have strained party unity.
Mr. Trump agreed to endorse Ms. Roby after appeals from Paul D. Ryan, the House speaker, and Kevin McCarthy, the House majority leader and Mr. Ryan’s possible successor. And even as he set aside older grievances against Ms. Roby, Mr. Trump underscored, by helping rescue her from political oblivion, that he is the dominant personality in the national Republican Party.
Several other Republicans who broke with Mr. Trump in 2016 have seen their political careers wither since then, partly as a result of deliberate score-settling by the president. Representative Mark Sanford of South Carolina, an insistent Trump critic, lost a Republican primary election last month; Mr. Trump urged voters to reject him on the afternoon of the election. And Senator Jeff Flake of Arizona, who refused to vote for Mr. Trump in 2016, abandoned his re-election campaign last year amid a series of clashes with the president that poisoned his relationship with Republican primary voters.
Still, Mr. Trump’s endorsements have not been a political cure-all for Republicans. He failed to carry another Alabama Republican, Senator Luther Strange, through a contested primary last year, and his intervention in a special congressional election in Pennsylvania in March did not stop Democrats from capturing a district that Mr. Trump carried easily in the presidential race.
In Alabama, if the charge of perfidy was politically dangerous to Ms. Roby, Mr. Bright, 65, was ill suited to the task of policing party loyalty.
Though he cast few liberal votes as a member of Congress, opposing the Affordable Care Act and other signature legislation backed by the Obama administration, Mr. Bright nevertheless served as a Democrat and voted to make Ms. Pelosi the speaker of the House.
Mr. Bright became a Republican to challenge Ms. Roby, disclosing that he backed Mr. Trump for president and vowing on his website to support the “America First” agenda. And he outpaced two more partisan Republicans in a June primary, finishing with 28 percent of the vote and earning a place in the runoff election against Ms. Roby.
Had Ms. Roby faced a more conventional Republican challenger, it could have complicated the national Republican Party’s efforts to keep her in office. Instead, top Republicans circled around a lawmaker who has been an uncomplaining ally of G.O.P. leaders, and who is one of only a few Republican women seeking re-election in the House.
In a Republican primary runoff for the state’s attorney general post, Steve Marshall, the incumbent, easily held off a challenge from Troy King after a bitter campaign that was briefly suspended after Mr. Marshall’s wife died last month.