Republicans survived an election scare on Tuesday and won a Kansas House seat in the first congressional election since President Donald Trump's victory, but the margin was much closer than expected in a district that had voted overwhelmingly for Trump in November.

Republican state Treasurer Ron Estes, 60, will represent the Kansas 4th congressional district replacing Mike Pompeo, who Trump named as CIA director.

Trump won 60 percent of the district's vote in November and Pompeo won re-election by 31 points. Estes defeated civil rights lawyer James Thompson by seven percentage points, suggesting some backlash against Republicans since November.

“Republicans nationally should be very worried,” said Bob Beatty, a Washburn University political scientist. “It's remarkable that Thompson got this close.”

The Kansas election was the first of four special elections to fill seats in the House of Representatives — where Republicans now hold a 237-193 majority— to replace Republicans who took top jobs in the Trump administration. Others are in Georgia, Montana and South Carolina.

Both parties will now turn their attention to Georgia and the extremely competitive April 18 contest to replace Tom Price, who resigned to serve as Trump's Health and Human Services secretary.

Democratic hopes rest with Jon Ossoff, 30, a former congressional staffer turned investigative filmmaker who has raised more than $8 million, an extraordinary amount for a special election. Ossoff is counting on opposition to Trump to propel him to a decisive victory.

The Kansas result also reflected blowback against the state's unpopular Republican governor Sam Brownback.

Thompson tapped into voter frustration after Brownback made Kansas a laboratory for sweeping tax cuts that left the state short of revenue and facing a budget crisis. Brownback also refused to expand the Medicaid health programme for the poor.

Lucy Jones-Phillips, a 31-year-old insurance representative and Democrat, acknowledged she doesn't vote in every election, but said she voted for Thompson because she wanted to register her disappointment in Brownback, especially his veto of an expansion of Medicaid.

“I can't stand Brownback,” she said as she left her polling site in Belle Plaine.

In a sign of nervousness in the waning days of the campaign, Republicans poured money into the race to bolster Estes. Republicans pulled in US Senator Ted Cruz of Texas to campaign in Wichita for Estes, and both Vice President Mike Pence and Trump recorded robo calls for him.

Those GOP calls prompted Charlene Health, a 52-year-old homemaker and Republican in Belle Plaine, to cast a ballot for Estes.

“I wasn't even going to vote,” she said as she left her polling site Tuesday morning. “I finally did. I realised this was important.”

Republicans have represented the south-central Kansas district since 1994. The district has been hurt by the downturn in the agricultural economy and the loss of hundreds of well-paying, blue-collar jobs in aircraft manufacturing plants. The 17-county congressional district includes the state's largest city of Wichita, home to Koch Industries, the company led by conservative billionaire political donors Charles and David Koch.

Estes handily won the district's rural counties. But Thompson won in Sedgwick County, which includes Wichita. Trump had carried Sedgwick county by more than 18 percentage points.

Estes said in his victory speech Tuesday night that his campaign showed political pundits and national media that they were wrong in suggesting he could lose the race.

The controversial orders Donald Trump has already issued Show all 9 1 /9 The controversial orders Donald Trump has already issued The controversial orders Donald Trump has already issued Trump and the media White House Press Secretary Sean Spicer takes questions during the daily press briefing Getty Images The controversial orders Donald Trump has already issued Trump and the Trans-Pacific Partnership Union leaders applaud US President Donald Trump for signing an executive order withdrawing the US from the Trans-Pacific Partnership negotiations during a meeting in the Roosevelt Room of the White House in Washington DC. Mr Trump issued a presidential memorandum in January announcing that the US would withdraw from the trade deal Getty The controversial orders Donald Trump has already issued Trump and the Mexico wall A US Border Patrol vehicle sits waiting for illegal immigrants at a fence opening near the US-Mexico border near McAllen, Texas. The number of incoming immigrants has surged ahead of the upcoming Presidential inauguration of Donald Trump, who has pledged to build a wall along the US-Mexico border. A signature campaign promise, Mr Trump outlined his intention to build a border wall on the US-Mexico border days after taking office Getty Images The controversial orders Donald Trump has already issued Trump and abortion US President Donald Trump signs an executive order as Chief of Staff Reince Priebus looks on in the Oval Office of the White House. Mr Trump reinstated a ban on American financial aide being granted to non-governmental organizations that provide abortion counseling, provide abortion referrals, or advocate for abortion access outside of the United States Getty Images The controversial orders Donald Trump has already issued Trump and the Dakota Access pipeline Opponents of the Keystone XL and Dakota Access pipelines hold a rally as they protest US President Donald Trump's executive orders advancing their construction, at Columbus Circle in New York. US President Donald Trump signed executive orders reviving the construction of two controversial oil pipelines, but said the projects would be subject to renegotiation Getty Images The controversial orders Donald Trump has already issued Trump and 'Obamacare' Nancy Pelosi who is the minority leader of the House of Representatives speaks beside House Democrats at an event to protect the Affordable Care Act in Los Angeles, California. US President Donald Trump's effort to make good on his campaign promise to repeal and replace the healthcare law failed when Republicans failed to get enough votes. Mr Trump has promised to revisit the matter Getty Images The controversial orders Donald Trump has already issued Donald Trump and 'sanctuary cities' US President Donald Trump signed an executive order in January threatening to pull funding for so-called "sanctuary cities" if they do not comply with federal immigration law AP The controversial orders Donald Trump has already issued Trump and the travel ban US President Donald Trump has attempted twice to restrict travel into the United States from several predominantly Muslim countries. The first attempt, in February, was met with swift opposition from protesters who flocked to airports around the country. That travel ban was later blocked by the Ninth Circuit Court of Appeals. The second ban was blocked by a federal judge a day before it was scheduled to be implemented in mid-March SANDY HUFFAKER/AFP/Getty Images The controversial orders Donald Trump has already issued Trump and climate change US President Donald Trump sought to dismantle several of his predecessor's actions on climate change in March. His order instructed the Environmental Protection Agency to reevaluate the Clean Power Plan, which would cap power plant emissions Shannon Stapleton/Reuters

Thompson, 46, a political newcomer backed by a group aligned with Senator Bernie Sanders' 2016 presidential campaign, said late Tuesday that he will run for the seat again in 2018.

“We came a lot closer than everyone thought we were going to be at the beginning of the race, because from the very beginning people wrote us off as having absolutely no shot,” Thompson's spokesman Chris Pumpelly said.

Libertarian Chris Rockhold trailed a distant third in the race.