Terry McAuliffe narrowly won the Virginia governor’s race Tuesday, defeating Republican Ken Cuccinelli II by piling up votes in parts of the state hit hard by last month’s federal government shutdown.

After acidly negative campaigns by both candidates, Mc­Auliffe — a former Democratic National Committee chairman and legendary political fundraiser who has never held elective office — took large majorities in Virginia’s population centers, especially the Washington suburbs and Hampton Roads.

But despite winning back the state’s top two positions, Democrats and Virginia’s 72nd governor will preside over a divided government and a restless, almost evenly split electorate. The fissures in Richmond involve deep uncertainty about how to interpret public opinion on President Obama’s health-care law and internal battles among Republicans over whether they threw away this election by nominating a ticket of hard-line conservatives.

“This race came down to the wire because of Obamacare,” Cuccinelli said in an emotional concession speech, telling supporters that despite his loss, “you sent a message to the president of the United States . . . that Obamacare is a failure. . . . We were lied to by our own government in its effort to restrict our liberty.”

McAuliffe pledged a “mainstream, bipartisan” approach, promised to reach out to every Republican legislator and quoted Thomas Jefferson’s comment to bitterly divided voters after being elected president in 1800: “Every difference of opinion is not a difference of principle.”

Terry McAuliffe narrowly defeated his Republican challenger Ken Cuccinelli in the battle for the Virginia governor's seat. (Courtesy of WRIC)

Gov. Robert F. McDonnell (R) congratulated McAuliffe and pledged a “seamless and smooth” transition. As of shortly before midnight, Cuccinelli had not called McAuliffe, according to an aide to the governor-elect.

In the contest for lieutenant governor, state Sen. Ralph S. Northam (D-Norfolk) easily beat E.W. Jackson, a political newcomer and minister from Chesapeake whose fiery rhetoric and hard-line positions on social issues led many Republican officials to keep their distance through the campaign.

In the race to succeed Cuccinelli as attorney general, state Sen. Mark R. Herring (D-Loudoun) led Sen. Mark D. Obenshain (R-Harrisonburg) by less than one percentage point with nearly all the votes counted. Both candidates said they were preparing for a recount. Republicans appeared to be well on their way to retaining control of the House of Delegates by a wide margin.

The ebullient McAuliffe, 56, offered a strikingly liberal program to a traditionally conservative state where an influx of well-educated, nonwhite and immigrant voters has starkly shifted the political calculus in the past decade.

McAuliffe won a contest in which many voters said they were voting for the Democrat mainly to take a stand against Cuccinelli, 45, whom McAuliffe portrayed as driven by faith and political philosophy to absolutist positions against abortion rights, same-sex marriage, higher taxes and a larger government role in health care.

Over the past two weeks, as Cuccinelli framed the election as a referendum on Obamacare, momentum shifted in his favor, said his campaign strategist, Chris LaCivita, who criticized the national Republican Party for giving up on its Virginia candidate too soon and paring back on funding over the past month.

“There are a lot of questions people are going to be asking and that is, was leaving Cuccinelli alone in the first week of October a smart move?” LaCivita said. “We were on our own.”

The troubled rollout of the federal government’s health-care Web site did give Cuccinelli a boost, said Senate Minority Leader Richard L. Saslaw (D-Fairfax). “It upset a lot of people. But let me tell you something: You know what they call a guy who wins the governor’s race by only one point? Governor.”

Neither candidate went over well with voters; individually and together, they drew the highest unfavorable ratings of any candidates for governor in the past two decades, according to Washington Post poll data. Comedy Central’s “Daily Show” billed the race as a choice between a heart attack and cancer. Some of the state’s largest newspapers either refused to endorse anyone or recommended a vote for someone who wasn’t even running.

McAuliffe won majorities among women, African Americans, moderates, college graduates, people who said they were affected by the shutdown, and both low-income and high-income voters, according to exit polling data. Cuccinelli won among white men, gun owners, people who believe abortion should be illegal and middle-income voters.

The Libertarian candidate, Robert Sarvis, did well among young voters, winning about a fifth of their vote, according to exit polling. But despite Cuccinelli’s warning that a vote for Sarvis would in effect be a vote for McAuliffe, survey data indicated that the third-party candidate drew fairly evenly from both major parties’ bases.

For months, Virginians sat through a barrage of TV ads in which women expressed fear that Cuccinelli would roll back their rights to abortion and birth control — a message that resonated with many after the General Assembly early this year passed a bill that would have required women to get an invasive, transvaginal ultrasound exam before having an abortion. (After a firestorm of protest, the law was revised to require a noninvasive ultrasound.)

As much as Cuccinelli protested that his campaign was really about reducing taxes to spur job growth, polls suggested that women believed otherwise; they preferred McAuliffe by a wide margin.

In the campaign’s final weeks, Cuccinelli bet everything on relentless criticism of Obamacare. Exit poll results found Virginians about evenly split in their view of the new health-care law.

In Manassas, Daniel Teddla, 36, a nurse, said he usually votes only in presidential elections but felt compelled to cast a ballot against extremism and in favor of the new health-care law.

“Cuccinelli is beyond conservative,” said Teddla, an independent. “He is too extreme.” He said he voted not so much for McAuliffe as against Republican attitudes toward women’s health and the Affordable Care Act. “Obamacare isn’t perfect, but they are not working to make it better. They are just totally against it.”

McAuliffe supports the health-care law, legalizing same-sex marriage and expanding Metro’s Silver Line into Loudoun County. He has promised to invest in transportation and schools and to protect the availability of abortion services.

Now, he faces the daunting prospect of governing with a still-heavily Republican House, where fiscal and social conservatives have pledged to block most spending or revenue initiatives. And the new governor will face questions about whether his struggling GreenTech Automotive business got special treatment from federal officials, the subject of two federal investigations.

The election of a Democrat one year after Obama won Virginia for the second time ends a streak of nine governor’s races, going back to 1977, in which Virginians elected the party opposite to the one that won the White House the year before. This also marks the first time since 1885 that any party has lost the governorship after just one term.

Democrats have now won three of the last four governor’s races, the last two presidential contests and the last three U.S. Senate elections. The GOP’s remaining power — it dominates the House of Delegates and the U.S. House delegation — is centered in rural and mostly white areas, leaving the party with a math problem: The regions where it is strongest are losing population, and places where it is weakest — particularly in Northern Virginia — are growing quickly.

McAuliffe’s victory comes after a strange political season in which the sitting governor stayed on the sidelines, the lieutenant governor refused to endorse his own party’s candidate, and Dwight Schar, once the finance chairman of the Republican National Committee, held a fundraiser at his McLean mansion for the Democratic candidate for governor, with Bill Clinton as his guest.

Bipartisan was never a word closely associated with McAuliffe during his years in Washington, where he was one of the most successful and flamboyant fundraisers in either party. As party chairman during the Clinton presidency, McAuliffe was for years the target of Republican rhetoric portraying him as a glad-hander who would do anything to raise money.

But this campaign presented a different McAuliffe, with his fun-loving and freewheeling side tamped down, his message disciplined, and his opportunities to improvise sparse. He barely spoke in his own TV ads, rarely gave news conferences and stuck to his talking points in public appearances.

McAuliffe, who grew up in Syracuse, N.Y., focused on national politics for most of his public career. He ran for governor in 2009 but was crushed in the primary and spent the next years visiting every community college in Virginia and campaigning for local Democrats.

Earlier in his career, McAuliffe found success and failure in various business ventures, always making a splash with his infectious smile, gleaming eyes and smooth chatter.

Some of his excesses came back to haunt him; Cuccinelli’s campaign relished using video of the Democrat, clad in a flowery shirt, waving a bottle of rum during a cable news appearance in 2008.

The Republican ticket, tagged by Democrats as the most conservative in modern history, included no nod toward moderates, a result of a fierce internal battle in which Cuccinelli’s tea party and Christian conservative backers forced a party convention rather than a primary. Conventions generally draw only the most devoted activists, whereas primaries are open to all voters, often leading candidates to position themselves toward the center.

In the last month, Cuccinelli focused more on turning out his base than on reaching out to independents. He campaigned with tea party favorites such as Sen. Marco Rubio (Fla.), Gov. Scott Walker (Wis.) and former congressman Ron Paul (Tex.).

Cuccinelli had to do without the usual assist from the sitting governor; McDonnell was largely absent, silenced by investigations into his relationship with a business executive who gave large gifts to the governor and his family.

Cuccinelli spent weeks trying to steer attention away from the gifts that he, too, received from the executive, Jonnie R. Williams Sr. of Star Scientific, which makes dietary supplements. Cuccinelli eventually bought TV time to announce he would donate $18,000 — the amount of the gifts he received from Williams — to charity.

Cuccinelli, who has spent 11 years in Richmond, was vastly outspent, especially in the past month; McAuliffe raised $34 million to Cuccinelli’s $20 million.

The Republican got no contributions from most of the major donors to McDonnell’s 2009 campaign. Some major GOP politicians also abandoned their party’s flagbearer, either sitting out the race or crossing the aisle to back McAuliffe.

Husna Khalil, a government worker who lives in Leesburg, said she and her husband — also a federal employee — are Democrats who have supported some Republicans in the past. Not this time.

“I’m here because of the shutdown,” Khalil said after casting her ballot for McAuliffe in Leesburg. “We didn’t vote in the last [gubernatorial election], but this time we came out. The shutdown makes nothing but problems. Even in other countries, our name becomes bad.”