CHARLOTTE, N.C. — Republican Dan Bishop narrowly defeated Democrat Dan McCready in a special election on Tuesday in North Carolina's 9th Congressional District.

The race was being closely watched for possible signs of weakness among Republican voters that could have spelled bad news nationally in 2020 for President Donald Trump, who endorsed Bishop and campaigned for him on Monday.

A special election was called in the district after last year’s results weren't certified due to allegations of fraud.

Bishop was ahead with 50.7 percent, or 96,004 votes, and McCready had 48.7 percent, or 92,066 votes, with about 98 percent of the tally in. NBC News projected Bishop as the winner shortly after 10:00 pm, ET.

Full results available here.

Democrats said Trump's unpopularity is the only reason the GOP-leaning district was competitive in the first place and that Republicans needed to pull out all the stops to win it. And, looking ahead to next year, Democrats say there are 34 Republican-held congressional districts that are more competitive than this one.

But Trump's rally and vocal support may have helped push Bishop over the line — and the president was quick to take credit for the win.

Dan Bishop was down 17 points 3 weeks ago. He then asked me for help, we changed his strategy together, and he ran a great race. Big Rally last night. Now it looks like he is going to win. @CNN & @MSNBC are moving their big studio equipment and talent out. Stay tuned! — Donald J. Trump (@realDonaldTrump) September 11, 2019

Bishop thanked Trump and other Republicans in his victory speech, saying his first act in Congress would be to help the president build a wall along the Mexican border.

"Under this president, America is great again," he said. "We stand for our flag and we kneel for our maker."

Bishop then pulled out his iPhone and called the White House from the podium to put the president on speakerphone so his supporters could hear.

"What he did tonight is monumental," Trump said as Bishop held his phone to microphone. "So congratulations!"

Every one of the dozen voters who spoke with NBC News in Indian Trail, a suburb of Charlotte, said the president was a factor in their vote.

"I recently changed my affiliation from Republican," said Dawn Leavesley, who voted for Trump in 2016, but has been disappointed with him. "Dan McCready promises to vote for the people, not for party, and even though I don't necessarily agree with either party on different things, I believe that he's the best man for the job."

Download the NBC News app for breaking news and politics

Both parties poured millions of dollars into the race, with Democrats spending about $6.8 million on TV and radio ads while Republicans spent about $6.5 million, according to data from Advertising Analytics.

The 9th Congressional District, which includes parts of Charlotte and Fayetteville suburbs and large swath of rural territory in between, has been held by Republicans since 1963. Trump won it by 12 percentage points in 2016.

The results point to ongoing trouble for both parties: Bishop lost ground in the suburbs while McCready underperformed in some of the district's rural areas.

McCready, a solar energy entrepreneur and former Marine, ran as a moderate who pledged to "put country over party" and rejected many the progressive policies of his party's presidential candidates.

"We were not successful tonight, but I want you to remember that victory postponed is not defeat," he told supporters Tuesday in his concession speech.

McCready made the contest in conservative district competitive thanks to a head start and the national undercurrent of blowback against Trump.

In last year's race, GOP nominee Mark Harris led McCready by 905 votes, but was never given a seat in Congress after allegations emerged that a consultant working for him had improperly handled absentee ballots.

A new election was ordered and Republicans chose a different nominee for the do-over: Bishop, a state senator who backed the state's controversial "bathroom bill," which prohibited transgender people from using the bathrooms and locker rooms that align with their gender identity.

While the race did not have major implications for the balance of power in the House, Republicans were aware that losing the seat would have required them to win one extra one — 20 instead of 19 — to reclaim the House majority in 2020.

Dan Bishop, the Republican candidate for senate in North Carolina's 9th District, speaks with supporters in Hope Mills on Aug. 10, 2019. Tom Williams / CQ-Roll Call file

Bishop and GOP outside groups field-tested the kinds of attacks they may use against other Democrats next year, especially on immigration.

"The results tonight are the clearest sign yet that the left's embrace of socialism is putting even their strongest and best-funded candidates in dire jeopardy," said Dan Conston, the president of the Congressional Leadership Fund, a GOP super PAC.

That argument worked for voter Richard Swindler, who said he wants his representative in Congress to have the president's back.

"I know that the other Dan (McCready) would have sided with Pelosi and that gang and Dan (Bishop) will probably side with Trump and his gang, and I believe that's the right side of the fence to be on," Swindler said.

Democrats had hoped a win in the traditionally Republican suburbs here would not only help protect their House majority, but energize the party heading into next year's presidential election.

"Tonight's razor-thin result in this ruby-red district solidifies the fact that Democrats are pushing further into Republican strongholds and are in a commanding position to protect and expand our House Majority in 2020," said Rep. Cheri Bustos, D-Ill., the chairwoman of the Democratic Congressional Campaign Committee.

Ninth Congressional district Democratic candidate answers a question during a news conference in Charlotte, N.C. on May 15, 2019. Chuck Burton / AP file

In a second special election in North Carolina on Tuesday, Republican Greg Murphy easily defeated Democrat Allen Thomas, according to an NBC News projection. The district's former incumbent, GOP Rep. Walter Jones, died earlier this year. Trump carried the district easily in 2016.

Alex Seitz-Wald reported from Washington, and Leigh Ann Caldwell reported from North Carolina.