NBC News projects Democrat Terry McAuliffe narrowly defeated Republican Attorney General Ken Cuccinelli in the race for governor of Virginia Tuesday, ending a race driven by negative ads, unrelenting accusations of dodgy behavior and a deep rancor between the rival candidates.

With 99 percent of precincts reporting, McAuliffe had 48 percent of the vote to 45 percent for Cuccinelli. Libertarian Robert Sarvis got about 7 percent of the vote.

“Look, I know this has been a hard fought race, and part of that, as you know, is the nature of politics, and part of it was that the attorney general and I had some very big differences on some very important issues,” a raspy-throated McAuliffe said in his victory speech.

He thanked Cuccinelli for his service and dedication to Virginia.

McAuliffe, a Democrat, ran strong among unmarried women, voters who made abortion a top issue and those who called the suburbs of Washington, D.C., home, according to preliminary results of an exit poll conducted for The Associated Press and television networks. Cuccinelli, meanwhile, fared well among tea party backers, gun owners and among the state's rural residents -- but there were not enough of them to yield a victory.

“Over the next four years, most Democrats and Republicans in Virginia want to make Virginia a model for pragmatic leadership that is friendly for job creation; a model for strong schools that prepare our students for the jobs of tomorrow; a model for welcoming the best and brightest scientists and innovators no matter your race, gender, religion or who you love; a model for a transportation system that reduces gridlock for our families and our business,” McAuliffe said. “All of this is only possible if Virginia is also the model for bipartisan cooperation and that’s a view that I share with the next lieutenant governor of the Commonwealth of Virginia, Ralph Northam.”

In winning, McAuliffe broke a stubborn streak in state history. During the past nine governor's races, the party that controlled the White House at the time has always lost.

From the outset, the campaign shaped up as a barometer of voters' moods and a test of whether a swing-voting state like Virginia could elect a tea party-style governor. As one of just two races for governor nationwide, political strategists eyed the race for clues about what would work for 2014's midterm elections when control of Congress is up for grabs.

McAuliffe, who once led the Democratic National Committee and is a confidant of former President Bill Clinton and Hillary Rodham Clinton, said he would expand Medicaid to provide health coverage for 400,000 people under the federal health care law. By contrast, Cuccinelli vehemently opposed the law and was the first to challenge it in court. He said that the narrow loss despite being heavily outspent during the campaign is evidence that Virginians don't want the Affordable Care Act.

“We said this race was a referendum on Obamacare, and although I lost, tonight you sent a message to the president of the United States that you believe that Virginia understands that Obamacare is a failure and that you want to be in charge of your healthcare and not the government,” said Cuccinelli, also in poor voice. “At the last count that I was aware of, despite being outspent by an unprecedented $15 million, this race came down to the wire because of Obamacare. ... That message will go out across America tonight.”

Turnout, however, was low, and both candidates worked through Election Day to reach as many people as possible.

NBC News also projects Democrat Ralph Northam defeated Republican E.W. Jackson in Virginia's controversial lieutenant governor race, which was defined by social issues. The campaign centered on Northam's defense of abortion rights and Jackson's Christian-based anti-abortion platform. Jackson also opposes gay marriage, while Northam supports it.

Throughout the campaign, Jackson was slammed for some controversial remarks, including comparing Planned Parenthood to the KKK. Jackson defended his remarks, telling the Free Lance-Star, "I say the things I say because I'm a Christian. It's not because I hate anybody. But because I have religious values that matter to me."

Northam and Jackson squared off in a lieutenant governor debate last month, tackling health care, how to care for the dangerously mentally ill and women's access to reproductive health services.

Northam becomes the first Democrat to occupy the office since U.S. Sen. Tim Kaine held the seat in 2006.

Jackson sounded anything but a loser as he rallied a downcast crowd at GOP election headquarters in Richmond. Saying he was "unbroken, unbowed," the lawyer-turned-preacher gave a stirring address Tuesday night.

The attorney general's race, however, is too close to call between two candidates with stark differences, and a recount appears likely. Republican Mark Obenshain has a long record as a soldier in the conservative culture wars while Democrat Mark Herring has been a loyal member of the minority in the closely divided Senate, arguing for abortion rights and tighter gun regulations.

While the morning went off without a hitch at most locations, some voters in Loudoun County ran into problems. Officials say 25 of the county's 85 precincts experienced a computer problem with the electronic poll books, which are used to check voters in as they arrive.

"We are working to resolve the networking problems that occurred early this morning with the laptops that we use to check in voters," said Loudoun County General Registrar Judy Brown.

Brown said every impacted precinct had at least one functioning electronic poll book, so residents were still able to vote.



Many went to Spring Hill Elementary School early to vote, News4's Melissa Mollet reported, but with only one electronic voting machine at Spring Hill Elementary, most made their vote count the old-fashioned way -- on paper.

Candidate McAuliffe was one of those voters.

Joined by his wife, Dorothy, and four of their five children, McAuliffe stopped by Spring Hill Elementary at 6:30 a.m.

McAuliffe later stood outside the Vienna/Fairfax-GMU Metro station, greeting commuters. He spent the rest of the day visiting campaign offices across the commonwealth to help sustain his lead in the polling.

Ken Cuccinelli and his wife, Teiro, stood side-by-side as they filled out their ballots a few hours later, at Brentsville High School in Nokesville. The Republican candidate made his own campaign visits throughout the day Tuesday, holding out hope his conservative supporters will fuel a come-from-behind win.



Robert Sarvis voted alongside his wife at Thomas Jefferson High School for Science and Technology in Alexandria at mid-day before heading to Richmond.

Preliminary results of an exit poll found the economy was the top issue for just about half of those going to the polls Tuesday, with health care a distant second, cited by one-quarter of voters. One in five called abortion the top issue. The exit poll included interviews with 1,520 voters from 39 polling places around the state.



The campaign's negative tilt turned many voters off -- “I think every single person in Virginia is glad that the TV ads are now over,” McAuliffe joked at the end -- and strategists in both parties predicted the outcome could be decided by just a few thousand votes.

McAuliffe's narrow victory in Virginia rested on a 9-point edge among women, while the two major party candidates split men about evenly, according to exit polls. McAuliffe carried liberals and moderates, Cuccinelli independents and tea party backers.



Libertarian Robert Sarvis spiked to 15 percent support among voters younger than 30, and independents.

Andy Anderson of Alexandria said he cast his vote for Sarvis as a means of "keeping Cuccinelli out of office. I really don't like McAuliffe either.... It'd be fun to shake things up in Virginia just a little bit."

Richard Powell, a 60-year-old retired IT manager who lives in Norfolk, described himself as an independent who frequently votes for members of both parties. He said he cast his ballot for McAuliffe, although not because he's particularly enthusiastic about him. He said he was more determined not to vote for Cuccinelli, who he said overreaches on a variety of medical issues.

Voters were barraged with a series of commercials that tied Cuccinelli to restricting abortions, and while Powell said the negative advertising “got to be sickening,” abortion rights played a factor in his vote.

“I'm not in favor of abortion -- let's put it that way -- but I find that restricting abortion causes far more social harm than allowing abortion, so that was an issue for me,” he said.

McAuliffe succeeds term-limited Gov. Bob McDonnell, a Republican, for a four-year term starting in January. Obama won the state in 2008 and 2012, but far fewer voters participate in off-year elections and that gives the GOP better odds.

Republicans bet a deeply conservative candidate would be their best shot, passing over a Lt. Gov. Bill Bolling for Cuccinelli, a crusader against the federal health care law. Democrats chose a loyal partisan who once led the Democratic National Committee and recruited the Clintons to raise millions for him and rally the party faithful.

In the weeks leading up to the election, McAuliffe led several polls, with Cuccinelli ranging from just a few points behind to 15 points behind.

Sarvis also attracted a lot of attention, garnering near double-digit poll numbers. Though some pundits said support came from possible Cuccinelli voters, Sarvis said he was a candidate bringing a new option to voters.

"We should all vote when we can, and the decision comes down to the platforms and who I feel will best represent my personal beliefs but also who's best for the country," said voter Joe Devylder of Vienna.

"I think it's a civic duty -- it's, 'Do I feel strongly about one candidate?'; it's 'I feel a duty to come out and express my democratic belief'," said Tim Slattery of Alexandria. "Because hey, if I don't come out and vote. then I can't complain about the next guy that's in office."

The outsize profile of Tuesday's election in Virginia -- one of just two states holding gubernatorial elections this year -- has been on full display as top-tier political figures shuttled into the state Monday to help McAuliffe and Cuccinelli make their closing pitch to voters.

Vice President Joe Biden revved up McAuliffe's volunteers in a backyard in Annandale, while Cuccinelli stopped in Warrenton for a rally with Sen. Marco Rubio of Florida.



But the governor's, lieutenant governor's and attorney general's races were not the only contests voters saw on their ballots.

Republicans turned back several challenges by Democrats as the GOP retained a solid majority in the Virginia House of Delegates. Democrat Jeremy McPike narrowly defeated Republican incumbent Scott Lingamfelter in unofficial returns Tuesday, but several other Republicans survived and the GOP kept 10 seats vacated by Republicans. No Democratic incumbents had lost, and results were still pending for a few of the 55 contested seats in the 100-member House.



Before a series of retirements, Republicans held a 67-32 edge over Democrats, with one independent aligned with the GOP caucus. Independent Lacey Putney retired, and his seat was won by a Republican.

In some municipalities, residents will cast their vote in a number of local elections, including the race for Fairfax County sheriff, which includes a female candidate for the first time.

Hoai-Tran Bui contributed to this report.