President Donald Trump had an outsized effect on Virginia's primary contests for governor, but not in the ways most people expected.

Democratic voters were energized in opposition to Trump and turned out in higher-than-expected numbers Tuesday. But the majority were happy to back the more moderate and establishment-favored Lt. Gov. Ralph Northam over a liberal insurgent who sought to portray his candidacy as the tip of the spear of the anti-Trump resistance.

The biggest surprise was on the Republican side. Former Republican National Committee Chairman Ed Gillespie, a moderate candidate who has kept his distance from Trump, squeaked out an uncomfortably close victory against a staunch Trump supporter who many viewed as a fringe candidate. Gillespie barely defeated Corey Stewart, who was fired as Trump's state campaign chairman last year and focused his gubernatorial bid on preserving Virginia's Confederate history.

Get Breaking News Delivered to Your Inbox

The general election is expected to be an early referendum on the president and a preview of what the 2018 midterm elections will look like. Virginia is one of only two states electing new governors this year, and the swing-state contest is likely to draw intense national scrutiny for signs of how voters are reacting to Trump's first year in office.

In his victory speech, Gillespie tried to play peacemaker and asked Stewart supporters to get behind him for the general election.

"We want you to be part of our effort," Gillespie said.

But Stewart, who frequently savaged Gillespie during the campaign as an out-of-touch former lobbyist, did not immediately concede the race.

"There's one word you will never hear from me and that's 'unity.' Because look, folks, we've been backing down too long ... in defense of our culture, our heritage and our country," Stewart said at a campaign event.

The close results shocked many political watchers and shows Trump's enduring appeal among Republican voters, as well as a party still facing significant internal strife between its establishment and anti-establishment wings. It also highlights a potentially rocky road for Gillespie and other Republicans in the general election in a state where Democrats have won every statewide contest since 2009 and Hillary Clinton won easily last year.

Stewart supporter Isaac Smith, 21, of Charlottesville, said the outcome was "remarkable" given the fact that Stewart was badly outspent. Smith viewed the results as a vindication of Trump. Smith added that he may vote for Gillespie, but won't campaign for him.

"Nobody's excited about Ed," he said.

But Kevin Felty, a Norfolk Republican, said he voted for Gillespie because of his fundraising prowess in what's likely to be a bloody general campaign. Felty, a 48-year-old surgical assistant, said a GOP victory in Virginia would wrestle back the "narrative being touted in the mainstream media that the Democrats are gaining traction" under Trump.

"I see this as the midterm of the midterms," said Felty, who voted for Trump and remains steadfast in his support. "I think it has bigger consequences than people imagine."

On the Democratic side, Northam wound up handily defeating former U.S. Rep. Tom Perriello, who ran as an unapologetic liberal crusader supported by prominent national Democrats like Sens. Bernie Sanders and Elizabeth Warren as the best candidate to take on Trump.

Northam, a usually low-key pediatric neurologist, won running as a pragmatist with the state Democratic establishment's firm support. He gave a fiery victory speech Tuesday, vowing to win over Perriello supporters and lead Democrats in retaking control of the state House of Delegates in the general election.

"It is time for us to get back on offense and stop playing so much defense," Northam said.

More than 560,000 people voted in the Democratic primary, a more than 240,000 voter jump from the last time there was contested primary in 2009.

Hyun Lee, 37, of Centreville, made calls and knocked on doors to turn out voters for Northam.

"I trust his leadership. I trust his dedication to all Virginians," Lee said of Northam. "He cares for everyone."

Perriello made a surprise entrance into the race in January and faced an uphill climb from the beginning. He energized many new-to-politics voters who oppose Trump but was ultimately unable to get enough new voters to counter Northam's advantages.

Northam essentially had been campaigning for years, making key contacts with influential power brokers like prominent African-American politicians and religious leaders, and building up a large cash advantage that let him outspend Perriello on TV advertising in the closing weeks of the race.

Perriello pledged in his concession speech to help Northam and said his campaign had shown that a "movement" is "rising up."

"I don't know about you, but I'm inspired to keep fighting tonight," Perriello said.