Updated at 1:10 a.m. ET on June 21

Republican Karen Handel has won the costly and closely watched special congressional election in Georgia's 6th District, a blow to Democratic hopes of pulling off an upset in a district that President Trump only narrowly carried last year.

The former Georgia secretary of state won by almost 4 points, beating Jon Ossoff, a 30-year-old documentary filmmaker and former congressional staffer — 51.9 percent to 48.1 percent.

The more than four-month-long battle smashed House race spending records after the candidates as well as GOP and Democratic outside groups poured in more than $50 million combined, slamming each other on the airwaves. Handel will succeed former Rep. Tom Price, whom Trump tapped as his secretary of Health and Human Services earlier this year.

The race had been framed by some as a referendum on Trump and his agenda, and the win may ease any GOP fears — for now — that its base wouldn't stay engaged heading into the 2018 midterms. Handel's win shows that even Republican voters who may have been reluctant to embrace Trump will still come home to vote for more traditional GOP candidates. It also suggests that attacks tying more centrist Democrats to the national party, particularly House Minority Leader Nancy Pelosi, can still prove effective.

Groups like the Congressional Leadership Fund, backed by House GOP leadership, spent heavily early on to try and knock down Ossoff, and it worked.

"Over the past month, Democrats not only declared this as a must-win, they all but guaranteed victory. After spending more than $30 million, Ossoff becomes the biggest loser in the history of Congress," CLF executive director Corry Bliss said in a statement.

"Nancy Pelosi threw the kitchen sink at her, yet Karen still came out on top and ready to fight for Georgia in Congress," National Republican Congressional Committee Chairman Steve Stivers, R-Ohio, said.

Trump also took to Twitter to praise Handel's win and celebrate Democrats' shutout in special elections.

The loss in such a high-profile contest has already started some finger-pointing from Democrats. Rep. Seth Moulton, D-Mass., who backed Ohio Rep. Tim Ryan over Pelosi in last year's leadership elections, tweeted that the defeat should be a "wake up call" for his party, which needed a new message.

Democrats have so far been unable to prove that they can turn their marches and protests against Trump into victories at the ballot box. The Georgia race was their best shot to do that, and they fell just short. Despite all the money spent on the Georgia race, Ossoff fell below Clinton's margin in the 6th District.

The race seemed to have tightened in the closing days. It's unclear how much a shooting at a Republican congressional baseball practice on June 14 might have impacted the race — though a neighboring district GOP chairman had said he believed the shooting could inspired GOP voters to come out to combat "left-wing extremism." Five people were injured in the incident, including House Majority Whip Steve Scalise, who remains in the hospital in serious condition. A little-known superPAC also made a small TV buy in the contest with an ad tying Ossoff to the to the alleged gunman, who appeared to be motivated by anger at the GOP and Trump.

In her victory remarks Tuesday night, Handel acknowledged the shooting and paid homage to Scalise, whom she said would frequently text her encouraging messages before he was injured. She said the tragedy should be a call for more civility in politics on both sides.

"We also need to lift up this nation so that we can find a more civil way to deal with our disagreements," Handel said. "Because in these United States of America, no one — no one — should ever feel their life threatened over their political beliefs."

Democrats badly needed a win somewhere after falling short in a handful of other special elections — including another one on Tuesday in South Carolina's 5th District that was much closer than expected. In that contest, Republican Ralph Norman beat Democrat Archie Parnell by just about 3 points to succeed Trump's Office of Management and Budget Director Mick Mulvaney.

The Palmetto State race received little national attention or money compared to Georgia or other special elections, but the narrow win compared to the 19 points Trump won the district by in November is a major swing. Similarly, there was a 20 point swing in a Kansas special election and 15 point shift in Montana last month.

As that anger with Trump rose across the country, Ossoff became a somewhat unlikely vehicle for the resistance to the president and the GOP agenda, attracting support from many people who said they were shell-shocked by Hillary Clinton's loss in November and who wanted to engage in politics. The first-time candidate raised more than $23 million for his campaign, but most of that came from outside of Georgia from liberal enclaves like New York and California. Volunteers from around the country flocked to help him on the ground, too, and with that money he was able to build an impressive ground game.

In the end, though, it wasn't enough, and the longtime GOP-lean of the district was too much for Ossoff to overcome. This is a seat Republicans have held for decades — former House Speaker Newt Gingrich even represented areas now in the current 6th District.

And even though this seat has been in GOP hands for decades, and Price routinely won re-election easily, this wasn't Trump Country in 2016. The president only narrowly carried the district by just over 1 point; in 2012, GOP nominee Mitt Romney won it by 23 points.

Still, it is the type of district Democrats have to do well in come 2018 to flip the 24 seats they need to win back the House. There are 23 districts currently held by Republicans which Hillary Clinton carried last November, and some of them share similar characteristics to Georgia's 6th District — rapidly growing, diverse district that are well-educated.

While special elections aren't a perfect predictor of future success in regular elections, Handel's win is still a disappointment for Democrats. However, as strategists point out, there are plenty of opportunities on the board for them in 2018, when a president's party typically loses seats. And it's futile to project what the environment may look like 17 months from now.

"There are more than 70 districts more favorable to Democrats than this deep-red district, and Ossoff's close margin demonstrates the potential for us to compete deep into the battlefield," Democratic Congressional Campaign Committee Chairman Ben Ray Luján, D-N.M., said in a statement. "The strong headwinds facing Republicans, incredible grassroots enthusiasm behind Democrats, and a damaged and exposed House Republican Caucus all clarify that we have the momentum heading into 2018."

While Ossoff successfully harnessed the anger against Trump to fuel his rise and his fundraising success, he also didn't make it the focal point of his campaign. Some of his ads early on ahead of the April all-party primary — where he got 48 percent of the vote in an 18-candidate field — talked about taking on the president. But later spots focused on cutting government waste and working to attract more tech jobs to the area. Those aren't typically the top Democratic talking points, and often he didn't even mentioned his party ID.

In his concession speech, Ossoff argued the enthusiasm his campaign was able to generate was still a good sign for the party in a race that wasn't expected to be close at the outset.

"We showed the world that in places where no one thought it was even possible to fight, we could fight," Ossoff told a gathering of disappointed supporters.

The outside money in the race became one of the most potent attacks by Handel, who was previously the Fulton County Commission chairwoman, the most populous area of the district that ultimately delivered decisive margins to help her finish off Ossoff. Handel emphasized her experience and decades of public service, tying Ossoff to national Democrats and pointing out that he didn't even live in the district. Ossoff grew up in the district but now lives just outside the border near Emory University, while his fiancee finishes medical school.

Handel also never fully embraced or distanced herself from Trump. The president held a fundraiser for her early on, and sent out tweets on her behalf in the final days of the race, including Tuesday night. But other surrogates like Speaker Paul Ryan and Vice President Pence were the ones who came to provide on-the-ground support. She thanked the president in her victory remarks on Tuesday night, getting loud cheers from her supporters.

The argument made by even Trump allies, such as Agriculture Secretary Sonny Perdue, the state's former governor, in the closing days was that even if you don't like Trump, don't hold that against Handel.

"Some Republicans may even be turned off by our president," Perdue acknowledged at a rally Saturday.

And, he seemed to admit, a loss could have be damaging for the Trump administration's agenda: "This is a harbinger of national politics. And the world is looking; the nation is looking."

Copyright 2017 NPR. To see more, visit http://www.npr.org/.