Democrats rode to victory in Tuesday's hotly contested gubernatorial elections, as Ralph Northam defeated Republican Ed Gillespie in Virginia and Phil Murphy beat Republican Kim Guadagno in New Jersey.

The victories -- especially the win in Virginia -- are a shot in the arm to the beleaguered Democratic Party, which has lost four special congressional elections to Republicans since Donald Trump was elected president a year ago.

Northam, Virginia's lieutenant governor, won with the support of federal government employees and non-white voters, according to the Fox News Voter Analysis.

Northam garnered 54 percent to Gillespie's 45 percent, with 99 percent of precincts reported.

As he began his victory speech late Tuesday night in Fairfax, Northam was briefly ushered off stage by security after several pro-sanctuary cities protesters in the crowd began heckling him. Once he returned to the stage, Northam vowed to work to unite the state.

“We need to close the wounds that divide, and bring unity to Virginia," he said. "Whether you voted for me or not, we are all Virginians. And I hope to earn your confidence and support as we move forward."

Speaking in Richmond, Gillespie told supporters he had conceded to Northam.

“Obviously, wish it had gone the other way, but I thank those who voted,” Gillespie said.

Democratic National Committee Chairman Tom Perez celebrated the wins by saying voters in Virginia and New Jersey “rejected a Trump-Pence agenda” -- though the voter analysis suggested President Trump was not a major factor at the ballot box.

Meanwhile, Trump reacted to the results by saying Gillespie, whom he supported, did not adequately embrace him during the race.

"Ed Gillespie worked hard but did not embrace me or what I stand for," the president tweeted. "Don’t forget, Republicans won 4 out of 4 House seats, and with the economy doing record numbers, we will continue to win, even bigger than before!"

Ahead of Tuesday’s elections, the president repeatedly tweeted his support for Gillespie in the race to replace Democratic Gov. Terry McAuliffe in Virginia. Polling in recent weeks had seemed to show Gillespie gaining ground on Northam in Virginia, the only southern state lost by Trump in 2016.

FULL ELECTION DAY COVERAGE

Towards the end of the campaign, Gillespie, a former chairman of the Republican National Committee, emphasized issues embraced by Trump during the presidential campaign, vowing to crack down on sanctuary cities and the MS-13 gang.

The race between Gillespie and Northam had gotten particularly nasty in recent weeks, with each side accusing the other of running ads that were out of line and racially tinged.

VIRGINIA GUBERNATORIAL RACE GUIDE

Last week, a liberal group called the Latino Victory Fund released a television ad showing immigrant and minority children in Virginia being chased down the road by a man in a pickup truck with a Confederate flag and a Gillespie bumper sticker. Republicans decried the ad, accusing Democrats of fear-mongering. The group behind it eventually took the ad down after last week’s terrorist attack by truck in New York.

Meanwhile, Democrats repeatedly protested ads from Gillespie accusing Northam of voting for bills that would lead to a threat increase from the MS-13 gang. In a video posted on Twitter after he voted Tuesday, McAuliffe warned about a possible Republican upset and called on voters to reject the “bigoted, racist ads that Ed Gillespie has run against Ralph Northam”

“I do not want you waking up like you did after the presidential election last November and saying, ‘how could this possibly happen?’” McAuliffe said.

VIRGINIA, NEW JERSEY GUBERNATORIAL CANDIDATES SPAR OVER SANCTUARY CITIES

Gillespie and Northam also sparred over the highly charged issue of removing Confederate monuments in the state. Northam's campaign attempted to tie Gillespie to the white supremacist violence this summer in Charlottesville, something Gillespie's campaign called an "ugly character smear."

Northam’s team portrayed the race as a referendum against Trump, labeling Gillespie “Trump's chief lobbyist” because of his former lobbying career.

The gubernatorial elections come as the national Democratic Party finds itself in disarray over former DNC interim chairman Donna Brazile’s new book claiming the DNC rigged the nomination for Hillary Clinton over rival Bernie Sanders in 2016.

In New Jersey, Murphy -- a former Goldman Sachs executive and U.S. ambassador to Germany -- will succeed outgoing Republican Gov. Chris Christie, who has seen his approval numbers take a drastic hit and is term-limited after eight years in office.

Christie's deep unpopularity made Lt. Gov. Guadagno's campaign an uphill climb from the start.

Like Gillespie, Guadagno spent the campaign railing against sanctuary cities, releasing an ad drawing attention to Murphy’s comments in support of New Jersey being a “sanctuary state” and telling the story of an illegal immigrant who was convicted of killing several students in 2007.

“Murphy doesn’t have our backs,” the ad’s narrator said. Referencing criminal illegal immigrants, the narrator added: “He has theirs.”

Like Northam in Virginia, Murphy responded by accusing Guadagno of channeling Trump.

“Kim Guadagno should be ashamed of herself for the way she’s politicizing the deaths of three children and painting a community with the broad brush of a murderer, a tactic she must have gleaned from President Trump,” Murphy said in response to the ad. “To say the least, these are not New Jersey’s values.”