By Alan Blinder, New York Times–

Senator Cindy Hyde-Smith, a Mississippi Republican who had to apologize for a cavalier reference to a public hanging, won a special runoff election on Tuesday, defeating the Democratic candidate, Mike Espy, who was trying become the state’s first black senator since Reconstruction.

Ms. Hyde-Smith’s victory, reported by The Associated Press, came in the final Senate race of the midterm elections and will set the Republican majority in the chamber at 53 to 47 once the new Congress is sworn in, a net pickup of two seats.

Teetering after several rhetorical gaffes drew a harsh spotlight to her campaign, Ms. Hyde-Smith received a last-minute boost from President Trump, who appeared at two rallies with her on Monday and cautioned Mississippians that a victory for Mr. Espy would also be one for Democratic leaders like Chuck Schumer and Nancy Pelosi.

The Republican win came as a deep relief to the party and Mr. Trump in a state where they rarely struggle, especially in Senate contests. Mr. Trump boasted repeatedly this year about his influence in helping his preferred candidates win elections, but the party had to go to unusual lengths — with the two rallies, multiple tweets by the president, a vast financial investment and dozens of Republican election workers dispatched to the state — to help Ms. Hyde-Smith over the finish line.

Her victory is clearly good news for Senate Republicans, who will now have an expanded, conservative majority to help advance Mr. Trump’s judicial nominees and negotiate with a Democratic-led House.

With 95 percent of the precincts reporting, Ms. Hyde-Smith had just over 54 percent of the votes.

“The reason we won is because Mississippians know me and they know my heart,” she said on Tuesday night. “This win tonight, this victory, it’s about our conservative values, it’s about the things that mean the most to all of us Mississippians: our faith, our family.”

Mr. Espy was the third prominent black Democrat to go down to defeat in a statewide race in the South this year, following losses by two gubernatorial candidates, Stacey Abrams in Georgia and Andrew Gillum in Florida.

Addressing supporters at the Mississippi Civil Rights Museum here less than three hours after the polls closed, Mr. Espy said he had conceded to Ms. Hyde-Smith. “She has my prayers as she goes to Washington to unite a very divided Mississippi,” he said.

Ms. Hyde-Smith’s election reinforced Republicans’ grip on power in Mississippi, a state they have come to dominate since the early 2000s, and showed that the political realignments taking shape in parts of the South are still in a nascent stage in Mississippi.

Still, the fact that Ms. Hyde-Smith faced a challenging runoff election, after no candidate received a majority of the vote on Nov. 6, suggested that Democrats could make select races competitive once again. And the frantic efforts to salvage her seat signaled that rhetoric seemingly steeped in Mississippi’s racist past risks a modern political price.

Although Ms. Hyde-Smith was never on a glide path to power — she faced a Republican rival and Mr. Espy in the first round of voting, all but guaranteeing Tuesday’s runoff vote — her campaign became more seriously imperiled through her own statements, including one in which she said that if a supporter invited her to “a public hanging, I’d be on the front row.”

Without that comment, and a handful of other controversial remarks, Democrats and Republicans alike said, Ms. Hyde-Smith’s victory on Tuesday would have been a near-lock.

Instead, Mr. Espy, 64, and his allies were able to seize on Ms. Hyde-Smith’s rhetoric and argue that it was an anachronistic representation of Mississippi, a state that has struggled mightily to repair its image more than a half-century after some of the gravest abuses of the civil rights era.

During a debate last week, Ms. Hyde-Smith, 59, who was the state agriculture commissioner until this year, said her “public hanging” remark reflected “no ill will,” and she asserted that she was being unfairly vilified.

Mr. Espy, a former agriculture secretary in the Clinton administration who was Mississippi’s first black member of Congress since Reconstruction, replied: “It came out of your mouth. I don’t know what’s in your heart, but we all know what came out of your mouth.”

Still, Mr. Espy refrained from attacking his opponent too strongly over her remarks, mindful of the large bloc of conservative white voters in the state who support Republicans and are deeply loyal to President Trump. It was a reflection of the balance Democrats need to strike as they try to make inroads in Southern states like Georgia and Texas, where appeals to the base of African-Americans, Hispanics and moderate suburbanites could alienate rural whites.

In region after region on Tuesday, Ms. Hyde-Smith was at her strongest in Mississippi’s rural and predominantly white counties, outpacing past Republican luminaries like Mitt Romney, the party’s presidential nominee in 2012. But in areas with greater numbers of college-educated white voters, such as the Memphis suburbs, she did less well, allowing Mr. Espy to draw closer than Democrats ordinarily do.

Ms. Hyde-Smith, who was appointed to the seat in April when Thad Cochran retired for health reasons, will now fill the remaining two years of his term. The seat will again be on ballot in 2020, when a six-year term will be at stake.

Mississippi is hardly accustomed to bruising Senate races between Democrats and Republicans. Senator Roger Wicker, who was also on the ballot on Nov. 6, won his bid for re-election this month with about 59 percent of the vote. (In 2014, Mr. Cochran survived a primary challenge and then trounced his Democratic rival in the general election.)

But in the three weeks between the first round of voting and the second, the matchup between Mr. Espy and Ms. Hyde-Smith became a nationally scrutinized test of Mississippi’s racial tolerance and the state’s standing as a conservative bulwark.

Although Mr. Espy announced his campaign months ago, it was not until the election’s closing weeks that he began to draw substantial national attention. But in Mississippi, speculation about Ms. Hyde-Smith’s strength as a candidate had swirled since March, when Gov. Phil Bryant named her as Mr. Cochran’s replacement, rejecting recommendations that he appoint himself.

Another Republican, Chris McDaniel, joined the contest, running only four years after he nearly defeated Mr. Cochran. Some Republican officials and strategists, including a few of the Legislature’s most influential members, publicly and privately questioned whether Ms. Hyde-Smith would be able to endure a well-financed campaign against her. Even the White House did not initially embrace Ms. Hyde-Smith, who they worried would falter in the race against Mr. McDaniel.

But Mr. Trump, betting on his enormous personal popularity in Mississippi, ultimately endorsed Ms. Hyde-Smith, who toured the state in a bus emblazoned with a picture of her and the president.

Speaking to reporters in Southern Mississippi on Monday, Mr. Trump tried to play down the “public hanging’’ remark that created so much of the firestorm that engulfed Ms. Hyde-Smith’s campaign.

“Really it was something that was sad and it was a little flip,” the president said after a round table on criminal justice legislation in Gulfport. “She called me, she said, ‘I said something that I meant exactly very different,’ and I heard an apology loud and clear.”

Democratic and Republican officials believed that Mr. Trump’s visit would stir up supporters of both candidates, after a first round of voting in which Mr. Espy won 40.6 percent, and Ms. Hyde-Smith took 41.5 percent.

At a glance, the memory and mathematics of Mississippi politics appeared to favor Ms. Hyde-Smith in a runoff: Republicans had been winning Senate races uninterrupted since the 1980s, and the party has won the last four campaigns for the Greek Revival governor’s mansion on East Capitol Street in Jackson.

And in Mississippi, which has the nation’s highest proportion of black residents but where about 60 percent of the voting-age population is white, political lines are often drawn in parallel to racial ones.

Mr. Espy needed a substantial turnout among black Mississippians, who made up more than a third of the voting-age population and historically sided with Democratic candidates. But Democrats also recognized that Mr. Espy needed to win about a quarter of the white vote; to that end, some of his advertisements evoked Mississippi’s long-running frustration with how it is regarded across the country.

But few people believed that Mississippi had many undecided voters in the campaign’s final days. The closing pitches from the candidates showcased the dueling approaches to the race.

At a church in Jackson on Monday night, Mr. Espy mixed outreach to black voters with a message he hoped would appeal to disaffected centrists, eschewing sharp partisan oratory while urging supporters to spend Tuesday “marching to the polls like it’s a holiday.”

And in Tupelo, with Mr. Trump at her side, Ms. Hyde-Smith eagerly wagered once more that an appeal to the right was the surest path toward political survival in Mississippi.

“I will stand for your conservative values,” she said, “and that’s what’s on the ballot.”