Democrats look set to hold Virginia's governorship after an ugly race, which has been seen as a political cardiogram of the US a year after President Trump's election.
Ralph Northam has beaten Republican Ed Gillespie after a campaign marked by tussles on immigration and Confederate statues.
Mr Gillespie rarely invoked the Republican president's name, though he tried to harness Mr Trump's populism.
The race could be a bellwether of next year's midterm congressional elections.
Mr Northam, the lieutenant governor of Virginia, was the front-runner though opinion polls had narrowed in the closing stretch, jangling Democratic nerves.
He and Mr Gillespie, a Washington lobbyist and former Republican party chairman, were vying to replace popular Governor Terry McAuliffe, a Democrat.
Democrats have been craving something to celebrate after defeat in four congressional special elections this year, despite a groundswell of grassroots opposition to President Trump.
The contest was the most closely watched out of a series of state and local races nationwide on Tuesday.
In New Jersey, Democrat Phil Murphy looks set to comfortably win the race to succeed unpopular Republican governor Chris Christie.
Virginia voters also made history by electing their first transgender state legislator.
Danica Roem, 32, ousted a pro-Trump incumbent Republican.
The swing state of Virginia has been trending Democratic in recent elections.
Virginians voted twice for former President Barack Obama, and Hillary Clinton carried the state by more than five percentage points last year.
Mr Gillespie, 56, had accused Mr Northam, 58, of failing to curb gang violence and seeking to tear down statues honouring Civil War, pro-slavery secessionists.
Mr Northam was seen as an anaemic campaigner after a series of flip-flops.
Progressive supporters were outraged after he reversed stance to say he would oppose an attempt by any Virginia city to provide sanctuary to illegal immigrants.
The Democrat offered to work with Mr Trump after previously calling him "a narcissistic maniac".
Mr Northam also vowed to lead efforts to remove Confederate statues, only to say later that he would leave the decision to local authorities.
The Northam camp was further embarrassed by a racially charged advert, released by a group supporting his candidacy.
It showed a white man in a pickup truck with a Confederate flag and a Gillespie sticker chasing down minority children.
The ad was quickly taken down after Mr Gillespie seized on it as proof that Democrats think all Republicans are racists.
Mr Gillespie, meanwhile, was accused of ducking questions about Mr Trump's controversies.
He did not campaign with Mr Trump, nor did he flaunt the president's tweeted endorsement.
Mr Trump, whose base is made up of white voters without university degrees, is not broadly popular in Virginia, according to opinion polls.
Well-educated urban professionals from the Washington suburbs to Richmond and the Chesapeake Bay have been building a stronghold for Democrats in the state.