U.S. Sen. Mark R. Warner (D) declared victory over Ed Gillespie (R) late Tuesday in a remarkably close contest for a second term that is likely to tarnish the Democrat’s image as an untouchable force in Virginia politics.

The contest was so close that Warner’s opponent declined to concede, but the Democrat promised to serve a second term working across the aisle with a new Republican majority in the Senate.

“It was a hard-fought race. It went a little longer than we thought,” Warner said. “I’ll work with anyone — Democrat, Republican, independent, you name it — if we’re going to make sure we get our country’s problems fixed.”

With nearly all the precincts reporting early Wednesday, Warner had 49 percent of the vote, just slightly ahead of Gillespie at 48.5 percent.

The outcome represented a shocking reversal of Warner’s sweeping victory in 2008, which first propelled the former governor into the Senate. A recount is possible, and Gillespie didn’t rule out requesting one in remarks to supporters late Tuesday.

A blue Virginia tide turns red for 2014

“Obviously, we are going to accept whatever is the final outcome,” the Republican said. “But I owe it to the voters of Virginia, owe it to all of you, to make sure that the outcome is final before we make any final decisions on this end.”

The race came against a national backdrop that was expected to be bad for Democrats, especially for incumbents with close ties with President Obama and his trouble-plagued health-care law. But Warner had been expected to escape that fate, with polls giving him an increasingly narrow but consistent lead through the final days of the race.

The moderate image that twice carried Warner to victory perhaps no longer plays well in a state and country that have grown more polarized since his race for governor in 2001. He lost support in rural areas that once solidly backed him while failing to inspire Democrats to turn out in droves.

This year, the senator saw his support in rural Virginia drop off sharply. He had forged ties to Southside and Southwest even before he ran for governor, earning goodwill in the economically depressed regions as a job-creating entrepreneur. Dave “Mudcat” Saunders, a Democratic strategist who helped craft Warner’s rural strategy for his gubernatorial run 13 years ago, attributed that drop to antipathy toward the president, in part racially motivated.

“It breaks my heart to say it, because these are my people, but racism was a huge factor in this,” he said. “I think in many areas of rural Virginia, racism is still prevalent, and they dislike Obama more than they like Mark Warner.”

Republicans have said the president’s policies and Warner’s support for them — especially a perceived “war on coal” and the Affordable Care Act — soured voters on both politicians.

“I like Gillespie. I think he’s a smart businessman. If I’m being truthful about it, I also like Warner. I just wish he was a Republican,” said Keith Davis, a retired Hanover County resident who voted for Gillespie. “Vote for him and you vote for Obama, and I don’t vote for Obama.”

Local election results

Gillespie’s chief line of attack was that Warner’s moderate image was just that — one that did not match reality. He said Warner had voted with the president 97 percent of the time, most notably for the Affordable Care Act.

That message seemed to convince voters that a man who has long billed himself as a business-friendly moderate was now too closely associated with national Democrats.

Late in the campaign, Warner also became tainted by a scandal related to the abrupt resignation of a Democratic state senator. Warner, one of several top Virginia Democrats who tried to dissuade the legislator from quitting the evenly divided state Senate, had discussed the possibility of a federal judgeship or private job for the lawmaker’s daughter.

Little national money has flowed to Virginia this year, in contrast to the tens of millions spent two years ago when Timothy M. Kaine (D) defeated George Allen (R). Warner attracted nearly twice as much as Gillespie in independent expenditures, and his campaign outspent Gillespie’s by the same margin. Gillespie, a prodigious fundraiser who helped create one of the country’s biggest super PACs, found himself running as the underfunded underdog. “Hard work beats big money every time, and we’re going to prove it tomorrow,” Gillespie told supporters at a rally Monday.

An onslaught of negative ads run by the Warner campaign in recent weeks hinted at concern that Gillespie was gaining ground.

Warner left the governor’s mansion in 2006 with record-high approval ratings, and in the race to succeed retiring Sen. John W. Warner (R) two years later crushed former governor James S. Gilmore III (R). He won with nearly two-thirds of the vote, dominating every region of the state, even rural areas where Republican presidential hopeful John McCain soundly beat Obama.

Victory would return Warner to a job that the Nextel co-founder has often found frustrating because of partisan gridlock. On the campaign trail, he has promised to devote the next two years to a forceful push for bipartisan solutions, saying he’s learned from a somewhat difficult experience.

Warner supporters say they appreciate his middle-of-the-road approach. “I think he’s his own man. He’s not a guy that’s going to let someone else’s opinion necessarily dictate his,” Daphne Steinberg of Fairfax said outside the senator’s election night party in Alexandria. “On the other hand, he’s a guy who listens to other people.” Despite being “richer than God,” Warner never seemed out of touch, she said.

Libertarian Robert Sarvis, who ran for governor in 2013 and won over a surprising 6.5 percent of voters, was less of a factor in this race, taking less than 3 percent of the vote in election night totals.

Brian W. Schoeneman, Fairfax County Electoral Board secretary, said county officials are already preparing for a possible recount.

“It’s getting tight,” Schoeneman said. “We’re in recount territory.”

In Virginia, the trailing candidate can request a recount if the difference between the two candidates is not more than 1 percent of the total votes cast. In this case, that would translate to roughly 20,000 votes.

Rose Mansfield, a spokeswoman for the Virginia Department of Elections, said there were problems in some parts of the state early Tuesday with how voting machines were displaying candidates’ names.

Election night results showed an electorate of over 2 million, about even with the 2010 election but far below the 3.8 million who came out to vote in 2012, the Department of Elections said.

Gillespie supporters expressed disappointment that the national Republican Party had not put more resources into the state.

“I don’t think the money was put behind [Gillespie] that should have been put behind him,” Carolyn Roy, a retired flight attendant from Virginia’s Northern Neck, said from the Republican’s election night party.

The elections also put Virginia’s new voter identification law to the test. Many members of voting rights groups were at the polls to monitor the process and help people comply with the new photo identification rule, among the nation’s strictest. The new requirement tripped up some voters, according to organizations monitoring polling places. By early afternoon, Tram Nguyen, co-executive director of Virginia New Majority, said she had not heard of any reports of voters being denied the right to vote because they lacked proper identification.

Also on the ballot in Virginia were all 11 congressional seats — including a hotly contested seat to replace retiring Rep. Frank R. Wolf in the Northern Virginia suburbs. Republican Barbara J. Comstock easily defeated Democrat John W. Foust — an outcome that may have hurt Warner in a portion of the state where he may have expected to perform well.