This article is more than
7 year oldProjections suggest Ralph Northam beat Republican Ed Gillespie after a campaign marked by tussles on immigration and Confederate statues.
Virginia voters also made history by electing their first openly transgender state legislator.
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.
In New York, Democratic Mayor Bill de Blasio was re-elected.
Meanwhile, Virginia voters elected their first openly transgender state legislator in Danica Roem.
The 32-year-old journalist and stepmother ousted a longtime, pro-Trump, incumbent Republican in a race that focused on traffic issues.
Her rival, Bob Marshall, described himself earlier this year as Virginia's "chief homophobe".
Analysis by Anthony Zurcher, BBC News, Washington
Democrats finally have a victory of the non-moral kind. After coming up short in a handful of special elections across the US, they went to the polls in a battleground state and posted a huge win.
How the Democrats, from governor candidate Ralph Northam on down, swept through election night in Virginia should be particularly concerning to Republicans across the US.
Turnout from Democratic supporters surged. They ran up huge margins with college-educated voters and residents in the wealthy Northern Virginia suburbs. The legions of rural voters who turned out for Donald Trump in 2016 were a non-factor.
Democrats won legislative races that were considered to be in play only in the rosiest of Democratic wave scenarios. Exit polls show a plurality of Virginians went to the polls to send a message to Mr Trump. Their top issue was healthcare. At least in Virginia, the president's dismal approval ratings translated into ballot-box poison.
The stage is now set for the midterm elections in 2018. Republicans will have a year to brace for what could be an anti-Trump tsunami forming on the horizon. What they - and Mr Trump - do next could decide their fate.
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.
Ed Gillespie worked hard but did not embrace me or what I stand for. 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!
— Donald J. Trump (@realDonaldTrump) November 8, 2017
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 rarely invoked the Republican president's name, though he tried to harness Mr Trump's populism.
Mr Gillespie did not campaign with Mr Trump, nor did he flaunt the president's tweeted endorsement.
The president tweeted after the result that the Republican lost because he "did not embrace me or what I stand for".
Mr Trump, whose base is made up principally 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.
Newer articles
<p>The deployment of Kim Jong-un’s troops has added fuel to the growing fire in recent weeks. Now there are claims Vladimir Putin has put them to use.</p>