WASHINGTON — Kentucky Attorney General Andy Beshear pulled off an apparent upset Tuesday night over Republican Gov. Matt Bevin, dealing a blow to President Donald Trump, NBC News projects.

And Democrats flipped both chambers of the Virginia state Legislature, giving the party complete control of the commonwealth for the first time in years. In Mississippi, Lt. Gov. Tate Reeves is the apparent winner in the governor's race over Democratic state Attorney General Jim Hood on Tuesday night, according to an NBC News projection.

Tate was endorsed by Trump, who campaign for him in Tupelo on Friday night. Tate had been favored in the red state, which has not elected a Democratic governor in 20 years.

In Kentucky, Trump couldn't save the unpopular Bevin after campaigning with him on the night before the election in Lexington, where the president told supporters that a loss by the GOP governor would be portrayed as Trump's having suffered "the greatest defeat in the history of the world."

With 99 percent of precincts reporting, the candidates were separated by less than 10,000 votes. Beshear was leading with 49.4 percent, or 706,865 votes, to Bevin's 48.7 percent, or 696,918 votes.

Bevin declined to concede on Tuesday night, citing unspecified "irregularities" in a speech to supporters, while Beshear declared victory, telling the crowd, "My expectation is that he will honor the election that was held tonight."

Under Kentucky law, candidates can request a recount but have to foot the bill for it.

Download the NBC News app for breaking news and politics

Turnout appeared to be higher than expected and is estimated at 1.4 million — roughly 400,000 more than the last governor's contest in 2015, according to an NBC News analysis.

The race was competitive from the start because Bevin is one of the least popular governors in the country, according to the Morning Consult poll, due in part to a history of incendiary comments and fights over public teachers and health care.

He tried to nationalize the contest and tie himself to Trump to overcome that headwind, with a closing campaign ad tying Beshear to "socialists in Washington (who) want to impeach Trump."

"Talk to the average person. Ask the next 100 people who come in here if they care about this impeachment process, and they will tell you almost to a person that they do because they find it to be a charade," Bevin said Tuesday at his polling place. "We don't appreciate when a handful of knuckleheads in Washington abdicate their responsibility as elected officials and try to gin up things that are not true because they can't handle the fact that Hillary Clinton didn't win."

Now, though, Republicans may begin to worry about their prospects in next year's elections if the president was unable to deliver his base in a state he won by 30 percentage points in 2016.

"You've got to vote," Trump told the crowd at Monday's rally. "If you lose, they are going to say Trump suffered the greatest defeat in the history of the world. You can't let that happen to me!"

Trump campaign manager Brad Parscale on Tuesday night tried to put the best spin on the outcome and suggested Bevin was a weak candidate, noting five of the six Kentucky Republicans running statewide won.

"The president just about dragged Gov. Matt Bevin across the finish line, helping him run stronger than expected in what turned into a very close race at the end," Parscale said.

Trump later tweeted much the same himself.

#ElectionNight Won 5 out of 6 elections in Kentucky, including 5 great candidates that I spoke for and introduced last night. @MattBevin picked up at least 15 points in last days, but perhaps not enough (Fake News will blame Trump!). Winning in Mississippi Governor race! — Donald J. Trump (@realDonaldTrump) November 6, 2019

Beshear, the son of the last Democratic governor in the state, Steve Beshear (who served two terms, 2007 to 2015), focused on bread-and-butter issues, including defending the Obamacare Medicaid expansion enacted by his father, and on his ability to work with Trump. But he aligns with national Democrats in support of abortion rights, putting him at odds with the bulk of Kentuckians.

"This is not about who is in the White House," Beshear said Tuesday before the polls closed. "It's about what’s going on in your house. It’s about the fact a governor can't affect federal policy but a governor can certainly impact public education, pensions, health care and jobs — four issues that Matt Bevin has been wrong on and we're going to do a lot of right."

President Donald Trump welcomes Mississippi Lt. Gov. Tate Reeves to the stage at a rally in Tupelo, Miss., on Nov. 1, 2019. Andrew Harnik / AP

Meanwhile, in Mississippi, with 91 percent of precincts reporting, Reeves was leading 53.8 percent to Hood's 45 percent, or 391,513 votes to 327,224 votes.

Hood had earned a nickname as the "last Democrat in Dixie" after winning four statewide elections as attorney general by sounding nothing like a national Democrat.

Hood's ads featured him hunting, repairing machinery and talking about God, and he's vowed to continue defending the state's strict new anti-abortion law in court if elected. "I bait my own hook. Carry my own gun. And drive my own truck," he says in one recent ad.

But it wasn't enough to overcome the state's heavily conservative lean, where Reeves portrayed Hood as a "liberal" and a "phony" who wanted to impeach Trump.

"All I know about Jim Hood is he fought very hard to elect crooked Hillary Clinton and Barack Hussein Obama," Trump said Friday at a Tupelo rally for Reeves. "He wanted Obama to win so badly and then he wanted Hillary to win, and that's not the kind of guy we need here, not Mississippi."

NBC News projects, based on an analysis of past election results, that Reeves is likely to satisfy Mississippi's unusual electoral law, which requires gubernatorial candidates to exceed 50 percent and also win at least 62 of the 122 State House Districts. The final vote results, however, may take two to three more days.

Reeves had nonetheless called Hood a "liberal and phony" who wants to take residents' guns, and a closing ad argued that Hood, as attorney general, sued Trump but "refused to challenge Obama, even one time."

Seitz-Wald reported from Washington, and Hillyard from Kentucky.