BROOKHAVEN, Ga. — President Trump’s hopes of steadying his presidency and his agenda on Capitol Hill were given a lift Tuesday when a Republican won a special congressional election in the Atlanta suburbs.

Republican Karen Handel defeated Democrat Jon Ossoff in Georgia’s 6th Congressional District, retaining a seat that has been in GOP hands since 1979 after a grueling, four-month campaign that earned the distinction of being the most expensive House race in history.

Handel won by almost 11,000 votes and by more than four percentage points, and Ossoff failed to reach the 48 percent mark that he topped in the initial round of voting in April.

Handel’s win will bring fresh attention to a beleaguered Democratic Party that has suffered a string of defeats in special elections this year despite an angry and engaged base of voters who dislike Trump.

It may also embolden Republicans in Washington to press ahead on an ambitious policy agenda that has yielded few legislative victories since Trump’s inauguration in January. Most immediately, the election result could bring momentum to Senate Republicans’ efforts this week to craft their version of a major revision to the Affordable Care Act.

“We need to finish the drill on health care,” Handel said during her victory speech here Tuesday. Chants of “Trump! Trump! Trump!” erupted before her.

Handel’s victory, however, revealed as much about Trump’s lingering problems among Republicans as it did the challenges facing Democrats. In a ruby-red district that her Republican predecessor won in November by 23 points, Handel struggled with Trump’s looming presence over the race. She won not with an embrace of the president but by barely mentioning his name.

“You showed the world that in places where no one even thought it was possible to fight, we could fight,” Ossoff, dressed in a black suit and black tie, told supporters Tuesday.

Handel, who will be the first Republican woman elected to Congress from Georgia, repeatedly ducked opportunities to echo Trump’s populist roar and instead presented a classic Republican case to voters, all while deflecting the barrage of questions about Trump’s latest tweets or his handling of investigations into Russian meddling in the 2016 election.

The Republican unease evident in the district could replay across the country next year, when both major parties are bracing for a bruising season of midterm elections at an uncertain national moment .

Back in Washington, party leaders — and Trump — paid close attention to the race. Inside the West Wing, Trump and his advisers were briefed regularly on Handel’s standing in private polls and Republican turnout, according to a White House official. In particular, the official added, strategist Stephen K. Bannon and chief of staff Reince Priebus were involved.

(Bastien Inzaurralde/The Washington Post)

“KAREN HANDEL FOR Congress,” Trump tweeted as day broke Tuesday, touting the Republican candidate and former Georgia secretary of state. “She will fight for lower taxes, great health care strong security — a hard worker who will never give up. VOTE TODAY!”

Handel and Ossoff vied to fill the seat vacated by Tom Price, who held it from 2005 until he joined Trump’s Cabinet this year as health and human services secretary. On April 18, Ossoff had nearly topped the 50 percent threshold that would have given him an outright victory in an 18-candidate primary field. Falling just short, he found himself in a runoff against Handel.

Ossoff, 30, a former congressional staffer, raised more than $23 million, built a devoted grass-roots following and courted Republicans by bemoaning “wasteful” spending.

In another Tuesday tweet, Trump took a swipe at Ossoff’s centrist positioning and dismissed him as a liberal who “wants to raise your taxes to the highest level and is weak on crime and security, doesn’t even live in district.” Ossoff lives just outside the district with his fiancee.

A record turnout was expected Tuesday: About 120,000 people cast early ballots, according to Georgia officials — nearly a quarter of registered voters here.

As Handel’s lead climbed late Tuesday, a senior White House official sent The Washington Post a text message: “They haven’t figured out how to beat Trump.”

For Democrats, Ossoff’s loss was demoralizing, coming after months of bitter infighting in the wake of Trump’s victory.

His defeat is also likely to lead to more criticism from the wing of liberal activists who want a more confrontational style embodied by Sen. Bernie Sanders (I-Vt.). They have already complained about the Democratic Congressional Campaign Committee’s willingness to support a more moderate candidate in Ossoff, while more progressive candidates in special elections in Montana and Kansas this year were left largely in the lurch.

Moreover, Ossoff’s loss raises real concerns about the continued potency of Republican attacks against Democrats by tying them to House Minority Leader Nancy Pelosi (D-Calif.). The anti-Ossoff campaign seemed to veer from issue to issue given the week, but the one constant thread over the last four months has been linking him to Pelosi.

According to one Republican involved in the effort, the Democratic leader had a name identification of 98 percent among voters in the Georgia district, and her disapproval ratings were 35 percentage points higher than her approval numbers.

Still, some Democrats said Ossoff’s competitive bid in Atlanta’s Republican suburbs could be a positive harbinger of next year, when they must win 24 GOP-held seats to reclaim the House majority.

“This is not the outcome any of us were hoping for, but this is the beginning of something much bigger than us,” Ossoff said during his concession speech. “Rather than demonizing each other, we find common ground and move forward.”

Democrats are likely to continue to view districts such as Georgia’s 6th as their roadmap to taking back the House — swing, suburban districts with well-educated populations and also more diverse electorates than in the poorer, rural districts that once served as the party’s foundation.

“We’re still going to be here,” Bill Atherton, 41, who works for a non-profit trying to transition low-income families into self sufficiency and attended Ossoff’s election-night party. “Now we believe we have a strong enough movement, not only to flip this district but inspire others.”

As the national political and media world focused heavily on the Georgia race, an underfunded, overlooked Democrat, Archie Parnell, also a first-time candidate, almost pulled off a huge upset in the South Carolina seat vacated by Mick Mulvaney, Trump’s budget director. Parnell lost by about 2,800 votes, a little more than 3 percentage points, after party leaders decided to devote all their attention in the run-up to Tuesday’s elections to the Ossoff-Handel race.

Of the four special elections prompted by Trump’s Cabinet selections, the DCCC identified the Georgia seat as vulnerable to the sort of political climate they expect to target next year. There are dozens of suburban districts with similar demographic makeup currently held by Republicans.

Despite the contest’s national sheen and implications, many voters here said they made their decision based less on Trump and more on how they view the two candidates, whose salvos have inundated televisions in a clash that has grown bitter and tense.

Jennifer Wilson, 52, a school counselor who went door to door for Ossoff on the eve of the election, said Ossoff’s age, as well as GOP attacks on his residency, were hurdles. “Some people say, ‘Oh, he’s only 30.’ But I tell them that Jon is someone who understands the area,” she said. “He grew up here and wants what they want: to bring high-tech and bio-tech jobs to our community.”

The Ossoff approach was to toe the middle of the road politically. His calls for civility, at a time of a nontraditional brand of politics from Trump, served as an indirect contrast to the president — a polite rebuke while trying not to offend those who voted for him.

“There is a great hunger here in Georgia, across the political spectrum, for leadership that is focused on civility, that is humble, that’s committed to delivering results instead of notching partisan wins or winning the day on Twitter,” he said Monday in an interview.

Handel supporters seemed genuinely puzzled by the attention the 6th District received, given its decades of support for Republican candidates, going back to Newt Gingrich, who began a long stint in 1978 when he won it while Jimmy Carter was in the White House.

Some, including Gingrich, largely rejected the suggestion that the contest was a referendum on Trump’s presidency.

“This is a referendum on if enough money can invent a person to win a special election,” the former House speaker said, taking a swipe at Ossoff as he watched returns on Tuesday. “He also backed off the whole model of a referendum on Trump. He figured out it wasn’t working.”

The national significance of the contest brought forth a flood of advertising and organization. Spending in the race by the campaigns and outside groups topped $50 million. The Congressional Leadership Fund, a super PAC affiliated with House Speaker Paul D. Ryan (R-Wis.), spent more than $7 million on its campaign against Ossoff and launched a field program.

Carolyn D. Meadows, a member of the board of the National Rifle Association, has lived in Cobb County her entire life and has been active in conservative politics since she was a “Goldwater Girl” during the 1964 election.

“No, we’re not a swing state, and we’re not a swing district,” said Meadows, who brought her granddaughter to Handel’s final event.

And on Tuesday, she was proven right, as Handel reasserted the political lines in suburban Atlanta in the age of Trump.