JACKSON, Miss. — 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 to 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.

The matchup between Mr. Espy and Ms. Hyde-Smith drew national scrutiny as a test of Mississippi’s past and present attitudes on race and its standing as a conservative bulwark, especially after the senator’s gaffe that she would be willing to attend a “public hanging.’’

Mr. Espy referred to the state’s history and its racial fault lines as he addressed supporters at the Mississippi Civil Rights Museum less than three hours after the polls closed. “She has my prayers as she goes to Washington to unite a very divided Mississippi,” he said.