Mark Obenshain campaigns Monday in Warrenton. (Nikki Kahn/The Washington Post)

The Virginia attorney general’s race was a virtual dead heat and headed for a recount early Wednesday morning, with Democratic State Sen. Mark Herring clinging to a 541-vote lead over Republican State Sen. Mark D. Obenshain with 2.2 million ballots cast, according to unofficial results posted by the state board of elections.

With 99.92 percent of the vote tallied, the margin between the two candidates was a scant .03 percent. State election law provides for the trailing candidate to request a recount if the margin is less than 1 percent of the total vote.

Speaking to reporters just before midnight, when Obenshain (R-Harrisonburg) was slightly ahead in the tally, Herring (D-Leesburg) said he would request a recount. But as Tuesday night turned into Wednesday, he issued a new statement:

“I am honored to have a majority of Virginians cast their ballots for me for Attorney General,” the statement said. “Just before 2 a.m., we took a several hundred vote lead to become the next Attorney General of the Commonwealth of Virginia.”

The chairman of the Virginia Republican Party, Pat Mullins, announced that Obenshain would make no comment because the party was preparing for a recount.

Obenshain appeared to be headed for victory early Tuesday evening on the basis of his double-digit advantage over Herring among independent voters. Among independents, preliminary exit poll data showed Obenshain with a 16 percentage-point lead over Herring. He also ran up big margins in solidly Republican central and western Virginia.

But Herring closed the gap, buoyed by big majorities in his Northern Virginia base. He won Fairfax County by 20 points and his home county of Loudoun by 6 points. He also won Arlington and Alexandria by margins of 3-1 and topped Obenshain handily in Norfolk and Newport News.

Polls before the election showed a tight race.

Obenshain ran a careful, relatively gaffe-free campaign to retain an office the GOP has held since 1994. His campaign glossed over divisive social issues and presented him as a less-polarizing figure than Republican gubernatorial candidate Ken Cuccinelli II or E.W. Jackson, the Republican nominee for lieutenant governor.

Obenshain was also able to keep his distance from the fallout generated by Gov. Robert F. McDonnell’s ethical scandals, which include state and federal investigations into luxury gifts for him and his family from Richmond area businessman Jonnie R. Williams Sr.

Cuccinelli also received $18,000 in gifts from Williams before donating their value to charity.

Obenshain also drew support from moderate GOP leaders who crossed party lines to endorse Terry McAuliffe (D) over Cuccinelli. Among them was Dwight Schar, part owner of the Washington Redskins and former national finance chairman of the Republican National Committee.

The attorney general’s race is traditionally a political sideshow that draws scant attention or funding. But this year’s contest moved closer to center stage when polls showed it to be the GOP’s only real shot at winning statewide office in 2013.

Late money poured into the race on both sides. Last week, the Democratic Attorneys General Association contributed $680,000 to Herring, according to the Virginia Access Project. Obenshain outraised Cuccinelli in the final weeks, receiving a $660,000 donation from the Republican State Leadership Committee.

Obenshain tried to steer his campaign around hot-button issues, building a message on broader conservative themes. He stressed noncontroversial positions, including tougher treatment of sex offenders and crackdowns on elder abuse and human trafficking.

Campaign ads attempted to soften his edges and appeal to women by featuring his family, including 20-something daughter Tucker, who said her father “encouraged me to be strong and independent in pursuing my dreams.”

Herring accused Obenshain of running away from his conservative record and depicted him as a Cuccinelli clone, especially in the area of abortion rights. Obenshain would “take the baton from Ken, build on his work, without missing a step,” Herring wrote in a Richmond Times-Dispatch column Oct. 26.

One Herring ad focused on a 2007 “Personhood” bill, co-sponsored by Obenshain and Cuccinelli, that banned most forms of contraception and outlawed abortion, even in cases of rape and incest. Herring also spotlighted Obenshain’s introduction of a 2009 bill — later withdrawn — that would have required women to report miscarriages to the police.

Herring also drew other contrasts, including his support for gay rights, the Affordable Care Act and a major state transportation funding bill to bring improvements to Northern Virginia, all of which Obenshain opposed.

Both candidates called for ethics reform and a limit on private gifts in the wake of the McDonnell scandal.

Obenshain, a Harrisonburg lawyer, is the son of Richard “Dick” Obenshain, who served as Virginia state Republican chairman from 1972 to 1976, when he was appointed co-chairman of the Republican National Committee. He was killed in a 1978 plane crash near his home in Chesterfield County after winning the U.S. Senate nomination. The party nominated John Warner as his replacement. Mark Obenshain’s mother, Helen Obenshain, went on to serve as Virginia’s Republican National Committeewoman.

During this year’s campaign, Obenshain often referred to a note found in his father’s desk after his death that said: “The most important goal in my life is to have a meaningful impact on preserving — and expanding — the realm of personal freedom in the life of this nation.”

Herring, a Leesburg lawyer, served one term on the Loudoun County Board of Supervisors before his election to the state Senate in 2005. As a state senator, he sponsored legislation to strengthen penalties for domestic violence and ban the sale of synthetic forms of marijuana.