Image copyright Getty Images Image caption Ralph Northam (L) and Ed Gillespie

Democrats have scored two significant victories in the US - in the first statewide elections since President Donald Trump came to power in January.

In Virginia, Ralph Northam defeated Republican Ed Gillespie in a governorship race marked by tussles on immigration and Confederate statues.

Phil Murphy, who beat Kim Guadagno, will be New Jersey governor.

The results could give an early indication of how next year's midterm congressional elections will turn out.

New York City's Democrat mayor was also comfortably re-elected.

Meanwhile, Virginia voters also made history by electing their first openly transgender state legislator.

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.

Overcoming the flip-flops

With nearly all the votes countered in Virginia, Mr Northam had 53.9% against 44.9% for Mr Gillespie.

Mr Northam, the lieutenant governor of Virginia, will now replace popular Governor Terry McAuliffe, a Democrat.

Mr Northam won despite a series of flip-flops during the campaign.

Progressive supporters were outraged after Mr Northam reversed stance to say he would oppose any attempt by a Virginia city to provide sanctuary to illegal immigrants.

The Democrat offered to work with President 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.

And his camp was further embarrassed by a racially charged advert, released by a group supporting his candidacy.

Mr Gillespie, a Washington lobbyist and former Republican party chairman, had accused Mr Northam of failing to curb gang violence and seeking to tear down statues honouring Civil War, pro-slavery secessionists.

Media playback is unsupported on your device Media caption Virginia election: Ed Gillespie running on Trumpism without Trump

Mr Gillespie's style was combative - but he distanced himself from President Trump.

Responding to the election result, Mr Trump tweeted that Mr Gillespie "did not embrace me or what I stand for".

Meanwhile, Virginia voters elected their first openly transgender state legislator in Danica Roem.

The 32-year-old journalist and stepmother ousted a long-time, 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".

Image copyright AFP Image caption Virginia voters also made history by electing their first transgender candidate

In New Jersey, Mr Murphy was comfortably leading the race with 55.5%. His Republican rival Ms Guadagno has 42.6%, with about 90% of the votes counted.

Mr Murphy will replace Republican Chris Christie, who was in office for eight years.

Reasons for Republicans to worry

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.

A year since Trump upset the odds