RICHMOND, Va. — Democratic Party insider Terry McAuliffe narrowly defeated Republican Ken Cuccinelli, a Tea Party favorite, to win the nationally watched Virginia governor’s race on Tuesday, television networks reported.

State election board results showed McAuliffe, a Democratic fundraiser and close friend of former President Bill Clinton, had 47 percent of the vote to 46 percent for Cuccinelli, Virginia’s attorney general, with 97 percent of precincts reporting.

McAuliffe squeaked to a win with a strong showing from wealthy, liberal-leaning Washington suburbs. His victory, called by networks CNN, CBS and NBC, cements Virginia as a bellwether swing state ahead of midterm congressional elections next year and the presidential election in 2016.

Record amounts of outside money flowed into the campaign as McAuliffe heavily outspent Cuccinelli and national Democratic figures attempted to make the vote a referendum on the Republicans’ small-government Tea Party wing.

Trailing in polls, Cuccinelli, 45, fought back in the final weeks in the campaign by attacking President Barack Obama’s signature health care program, which is off to a rocky start.

McAuliffe, 56, tied Cuccinelli to last month’s federal government shutdown, blamed by most Americans on Republicans and especially the Tea Party wing. Virginia was hit hard by the shutdown since it relies more than most states on federal paychecks and contracts.

Cuccinelli also was hurt by a scandal involving Virginia’s Republican governor, Bob McDonnell, who is under investigation for taking gifts from a businessman. Cuccinelli apologized in September for taking gifts from the same businessman.

Under state law, McDonnell could not run for a second consecutive term.

“All the things that could go wrong for the Republicans did go wrong. What was striking was how close it was with all the headwinds the Republicans had,” said Stephen Farnsworth, a pollster at the University of Mary Washington in Fredericksburg.

Libertarian Robert Sarvis, who campaigned on antipathy to the major parties and their candidates, notched 7 percent of the vote. It was the best showing by a third-party candidate in the state since 1965.

McAuliffe, who has said he has 18,632 names on his Rolodex, raised about $34 million to Cuccinelli’s $20 million, according to the non-partisan Virginia Public Access Project, which tracks political money in the state.

McAuliffe also outspent Cuccinelli 10-to-1 on television advertising in the last few weeks of the campaign, the Access Project said.

Underscoring the national interest in the race, about 70 percent of the money raised came from outside the state. That is by far the highest percentage for any U.S. gubernatorial race in history, according to the nonpartisan National Institute on Money in State Politics, in Helena, Montana.

Democrats also scored a win in the race for lieutenant governor, with Ralph Northam, a 54-year-old state senator from Norfolk, easily defeating Republican E.W. Jackson, a 61-year-old minister from Chesapeake.

Conservative Republican state Sen. Mark Obenshain also was leading Democratic state Senator Mark Herring in the race for attorney general.