By Jonathan Martin, New York Times–

Democrats won complete control of the Virginia government for the first time in a generation on Tuesday and claimed a narrow victory in the Kentucky governor’s race, as Republicans struggled in suburbs where President Trump is increasingly unpopular.

In capturing both chambers of the legislature in Virginia, Democrats have cleared the way for Gov. Ralph S. Northam, who was nearly driven from office earlier this year, to press for measures tightening access to guns and raising the minimum wage that have been stymied by legislative Republicans.

In Kentucky, Gov. Matt Bevin, a deeply unpopular Republican, refused to concede the election to his Democratic challenger, Attorney General Andy Beshear. With 100 percent of the precincts counted, Mr. Beshear was ahead by 5,100 votes.

Mr. Beshear presented himself as the winner, telling supporters that he expected Mr. Bevin to “honor the election that was held tonight.”

“Tonight, voters in Kentucky sent a message loud and clear for everyone to hear,” Mr. Beshear said. “It’s a message that says our elections don’t have to be about right versus left, they are still about right versus wrong.”

Mr. Bevin asserted to supporters that “there have been more than a few irregularities,” without offering specifics.

Mr. Bevin’s troubles did not appear to be a drag on other Republicans, who captured every other statewide race in Kentucky — a sign that Kentucky voters were rejecting Mr. Bevin and not his party. Daniel Cameron handily won the attorney general’s race, becoming the first African-American to claim the office and the first Republican to do so in over 70 years.

Republicans did manage to capture the governor’s mansion in Mississippi as Lt. Gov. Tate Reeves defeated Attorney General Jim Hood by seven percentage points in an open-seat race that illustrated the enduring conservatism of the Deep South. The final governorship up for grabs in these off-year campaigns is in Louisiana where Gov. John Bel Edwards, a Democrat, is facing re-election a week from Saturday.

In New Jersey, a state that seemed to be shifting increasingly blue each year, Republicans were on the cusp of their first legislative gains in nearly a decade. With final results still being tallied late Tuesday, Republicans looked likely to pick up two seats in the Assembly and one in the Senate, powered largely by a surge along the southern part of the state where Mr. Trump won easily in 2016 despite Democrats’ local advantage.

Across the nation Tuesday, a handful of candidates made history. In addition to Mr. Cameron in Kentucky, Ghazala Hashmi, a Democrat, was the first Muslim woman elected to the Virginia Senate, capturing a suburban Richmond district. And in Arizona, Regina Romero was headed toward victory in the Tucson mayor’s race, becoming the first woman and first Latina to lead that city.

In Virginia, where Mr. Northam and two other statewide Democrats were pressured to resign following a series of scandals earlier this year, the party overcame its own self-inflicted challenges by harnessing voter antipathy toward Mr. Trump to win a series of seats. For the first time since 1993, Democrats control both chambers in the legislature and the governor’s office — allowing them to redraw the state’s legislative boundaries after next year’s census.

Linking Republican incumbents to the unpopular president and criticizing them for opposing gun control measures in the aftermath of a mass shooting in Virginia Beach in May, Democratic challengers built their victory with strong showings in suburbs stretching from outside Washington to Richmond and Hampton Roads. In Fairfax County, the state’s largest jurisdiction, the last remaining Republican lawmaker was defeated.

Ten years after Republicans last won a statewide election there, the legislative victories cemented Virginia’s evolution to becoming a reliably blue state.

Mr. Northam, who admitted and then denied wearing blackface as a young man, said Tuesday night that Virginia voters made clear they “want us to defend the rights of women, L.G.B.T.Q. Virginians, immigrant communities and communities of color.” And he vowed to broaden access to health care, improve public schools, combat climate change and pass gun control legislation.

On a day of state and local elections that illustrated the country’s growing polarization, red-state Republicans sought to frame their campaigns as a test of loyalty to Mr. Trump while Democrats in more liberal states tied their opponents to the president.

Coming one year before the presidential election, the races reflected the country’s increasingly contentious politics and the widening rural-urban divide.

Nowhere was that more apparent than in Kentucky, where Mr. Beshear ran far better than national Democrats in the state’s lightly-populated counties but built his advantage thanks in large part to his overwhelming strength in the state’s cities and suburbs.

Mr. Beshear’s performance demonstrated that Mr. Trump’s popularity alone is insufficient for most Republicans, even in one of the most conservative regions in the country. Mr. Bevin and national G.O.P. groups, grasping for ways to overcome Mr. Bevin’s weakness, sought to turn the election into a referendum on Mr. Trump, national policy issues and the Democratic impeachment inquiry.

And the president himself stood alongside Mr. Bevin Monday night in Lexington to argue that, while the combative governor is “a pain in the a**,” his defeat would send “a really bad message” beyond Kentucky’s borders.

But three years after handing the president a 30-point victory, Kentucky’s voters appeared to put their displeasure with the conservative Mr. Bevin, his controversial policies and even more controversial personality, over their partisan preferences.

While Mr. Beshear’s apparent margin was slim, the result may have caught Mr. Bevin by surprise. In an interview near the end of the race, Mr. Bevin claimed the race was not even competitive and predicted he’d prevail by “6 to 10 percent.”

Mr. Beshear, a 41-year-old moderate whose father preceded Mr. Bevin in the governor’s mansion, sidestepped questions about Mr. Trump and impeachment while keeping his distance from national Democrats. He focused squarely on Mr. Bevin’s efforts to cut Medicaid and overhaul the state’s pension program while drawing attention to the governor’s string of incendiary remarks, including one that suggested striking teachers had left children vulnerable to molestation.

Yet even as he sought to steer a middle path, Mr. Beshear benefited from liberal enthusiasm, running up wide margins in the state’s two largest cities, Louisville and Lexington.

In a characteristically truculent Twitter thread on Tuesday as voting was underway, Mr. Bevin snapped at the “historically challenged national media” for being surprised at the competitiveness of the Kentucky race, pointing out that only four Republicans had been elected governor since the 1920s and that registered Democrats in the state still outnumbered registered Republicans. He did not mention that this partisan registration gap has considerably shrunk in recent years, nor that Mr. Trump romped there three years ago.

The elections Tuesday featured only a handful of statewide and legislative races, but they neatly captured how thoroughly polarized politics has become in the Trump era.

In the three governors’ races, Republican candidates linked themselves to Mr. Trump at every turn, joining him for rallies in their states and assailing their Democratic rivals for their party’s effort to impeach the president.

While Mr. Trump was embraced by Republicans, the Democratic standard-bearers in the races shunned their more liberal presidential contenders and refused to support the impeachment inquiry, not wanting to fuel the G.O.P.’s strategy of making the red-state races a referendum on the president.

Yet in Virginia, the only Southern state Mr. Trump lost, it was Republicans who were distancing themselves from their national party and a president who has alienated the suburban voters they needed to retain control of the state legislature. While the president stayed away from Virginia, just across the Potomac from the White House, every major Democratic presidential hopeful was welcomed with open arms to campaign with the party’s candidates. in a state that has not elected a statewide Republican in a decade.

In all four states, television commercials and campaign mailers were filled with mentions of Mr. Trump (positively and negatively) as well as of national Democratic leaders such as Speaker Nancy Pelosi, Senator Bernie Sanders and the so-called Squad of freshman House Democrats (negatively). And the same hot-button issues that have consumed a gridlocked Washington in recent years have also played a central role in races that in the past would have been dominated by talk of taxes, transportation spending and education.

Predictably, it was the Democrats in the red states and Republicans in increasingly blue Virginia who gamely sought to localize the races. Mr. Beshear and Mr. Hood hammered their Republican opponents on their records and issues unique to Kentucky and Mississippi while casting themselves as pragmatists with little allegiance to their national party. Suburban Virginia Republicans focused on their dedication to constituent service, including filling potholes, and trumpeted their willingness to break from party orthodoxy on some issues.

In Kentucky, Mr. Bevin’s inflammatory conduct — he once portrayed striking teachers as accessories to the sexual assault of children — appeared to have persuaded some voters, from both parties, to vote for Mr. Beshear. John Brown, who has worked in heating and air-conditioning for more than 30 years, said that he has wavered between parties over the years. This time, he voted for Mr. Beshear. “I watch the news, and that’s how I vote,” he said.

“He has poor manners,” Mr. Brown, 62, said, adding that he does not care for his hotheaded temperament, which was apparent when Mr. Bevin spoke. “You can tell his blood pressure is rising.”

Rick Rojas and Campbell Robertson contributed reporting from Louisville, Ky., and Nick Corasaniti from Miami.

Rick Rojas contributed reporting from Louisville, Ky.