Republicans have won a fiercely contested special election in North Carolina, narrowly escaping what could have been a serious setback for Donald Trump and his 2020 re-election campaign.

The conservative state senator Dan Bishop beat out the Democrat Dan McCready, a former marine who earned a master’s degree in business from Harvard, in a race that remained neck-and-neck for most of the night. Bishop had 50.8% of the vote to McCready’s 48.8% as of late Tuesday evening, with 99% of precincts reporting.

Bishop is perhaps best known for co-sponsoring the state’s “bathroom bill”, which forced transgender people to use restrooms that matched the gender listed on their birth certificates in state-owned buildings. The law was repealed after it prompted a national outcry and boycotts.

The contest in a usually reliably Republican seat had attracted national attention, including several campaign visits by Trump himself, and had increasingly been seen by Republicans and Democrats alike as a proxy battle for next year’s race for the White House. Republicans had dumped at least $20m into the ninth district race in a bid to save the seat from going blue.

The election caps a months-long political scandal, in which allegations of ballot tampering in the 2018 midterms forced state officials to void the results and order a fresh election.

At the time McCready lost the congressional district by just 905 votes to the then Republican candidate, Mark Harris. However within days it emerged “ballot harvesters” had been hired by a political operative, to pick up absentee ballots in Bladen county. Some of those ballots never turned up.

The operative, Leslie McCrae Dowless, was arrested and charged in February after grand jury indictments alleged the illegal possession of absentee ballots and obstruction of justice

The race was seen as a sign of Trump’s popularity – or lack thereof – in key districts he easily carried in 2016. The president won the ninth district by 11 points back in 2016’s general election and its loss would have been seen as a portent that Republicans were losing support in previously reliable heartland districts. Republicans have held the seat since John F Kennedy was president in 1963.

The race also gained notoriety in July when Trump staged a rally for Bishop in Greenville. Trump said four Democratic women of color should “go back” to their home countries, though all but one was born in the US. The crowd began chanting “send her back”, and Trump did not try to stop them, triggering an anguished national debate over racism.

Trump held another rally in North Carolina on Monday and threw his weight behind Bishop in a tweet on Thursday.

NORTH CAROLINA, VOTE FOR DAN BISHOP TODAY. WE NEED HIM BADLY IN WASHINGTON! — Donald J. Trump (@realDonaldTrump) September 10, 2019

Special elections generally attract such low turnout that their results aren’t predictive of general elections, especially in presidential years. Even so, Tuesday’s vote was scrutinized for signals about GOP erosion, particularly among women and college-educated people who have abandoned Trump in droves over his conservative social policies and vitriolic rhetoric on immigration and race.

Trump will need to stem his erosion in suburbs to ensure that his solid support from his rural and conservative, diehard loyalists is sufficient for him to carry swing states in the midwest and elsewhere like Arizona and North Carolina. He won North Carolina by less than four percentage points in 2016.

The election between McCready and Bishop was not the only House race in the state on Tuesday. In the third congressional district, where a seat was left open after congressman Walter B Jones Jr died in February, the Republican Greg Murphy, a physician, decisively defeated his Democratic opponent Allen Thomas.

Maanvi Singh and agencies contributed reporting