Democratic Va. attorney general candidate Mark Herring speaks with reporters about the vote count during an election night party in Tysons Corner on Nov. 6. The state Board of Elections on Monday certified Herring as the winner. (Cliff Owen/AP)

RICHMOND — The State Board of Elections on Monday declared Democrat Mark R. Herring Virginia’s next attorney general, capping a dramatic three-week certification process in the closest statewide race in Virginia history.

Herring defeated Republican Mark D. Obenshain by a mere 165 votes out of more than 2 million cast, according to the final tally certified in Richmond on Monday, at least temporarily giving Democrats a historic sweep of statewide offices. Herring joins Gov.-elect Terry McAuliffe and Lt. Gov.-elect Ralph S. Northam in preparing to assume office in January.

Yet the exceedingly narrow results also offered reason to brace for a recount. The certified tally gave Herring 1,103,777 votes to Obenshain’s 1,103,612 — a winning margin of less than one hundredth of a percent.

Obenshain did not immediately call for a recount, but he has set up a transition team, and his campaign issued a statement Monday noting that there have been four statewide elections across the country since 2000 with margins within 300 votes — three of which were reversed by recounts.

“Margins this small are why Virginia law provides a process for a recount,” said Obenshain’s campaign manager, Chris Leavitt. “However, a decision to request a recount, even in this historically close election, is not one to be made lightly.”

Obenshain, a state senator from Harrisonburg, has 10 days to request the recount. A recent change in state law would render it a huge undertaking, one that would probably stretch well into December and keep the identity of Virginia’s chief law enforcement officer in limbo even as McAuliffe and Northam move ahead with their transitions.

Herring, a state senator from Loudoun County, issued a statement thanking elections officials for their work.

“I am gratified that the State Board of Elections today certified me the winner of a close but fair election,” he said. “I look forward to serving the people of Virginia as Attorney General.”

Herring and Obenshain were vying to succeed Attorney General Ken Cuccinelli II (R), who ran unsuccessfully for governor against McAuliffe. Should Herring’s victory stand, Democrats would hold all five statewide offices — including both U.S. Senate seats — for the first time since 1970.

The state elections board voted unanimously to sign off on the results, ending a topsy-turvy 20-day process that began with Obenshain narrowly ahead and ended with him narrowly behind. Since the Nov. 5 election, cities and counties across Virginia have counted provisional ballots, certified their tallies and discovered a few errors that whipsawed the vote totals — and the candidates.

Some of the shifts were from mistakes attributed to human or machine error. Some were the result of the standard canvassing process that takes place after every election. The most significant discovery came in Fairfax County, Virginia’s largest jurisdiction, where officials discovered nearly 2,000 uncounted ballots on a single optical-scan voting machine.

In a typical year, these additions and subtractions don’t affect the outcome. This year, the contest for attorney general was so close that the normal process of fixing errors and counting provisional ballots caused the results tally to narrow dramatically in an already close race.

Adding to the drama Monday was the fact that Charles E. Judd, chairman of the State Board of Elections, voted to certify the results Monday but “with question” — primarily because of issues in Fairfax County.

Judd, the former executive director of the Republican Party of Virginia, was concerned that vote totals shifted repeatedly during the canvass in Fairfax.

“The numbers were changing. ‘Whoops, we found another mistake,’ then they changed the numbers. Then we find another mistake, we change the numbers,” he said.

Like every jurisdiction across the state, Fairfax gave voters who came to the polls without proper identification until noon on the Friday after the election to provide ID, Judd said that Fairfax officials have assured him. But Fairfax extended the deadline for a week after the election for voters who cast provisional ballots for other reasons, such as questions about their registration, Judd said.

“I’m concerned about the lack of uniformity, that there be no differences in any of the localities in how votes are counted,” he said.

Finally, Judd said he was concerned by reports that absentee ballots were not handled consistently across the state, with some jurisdictions counting those that did not bear the voter’s signature and others not.

He said the board is investigating those issues.

“Is it going to change the outcome of the election? No,” he said. “What we want to do is get this and find out what we can learn from it and see what can we do to help the process so this confusion goes away in the future.”

Brian W. Schoeneman, secretary of the Fairfax Electoral Board and also a Republican, said the county observed proper voting procedures.

“What we all need to be careful of right now is putting questions in the minds of voters as to whether this process was handled correctly and fairly,” he said. “From my perspective, the process worked right — errors were caught, and voters were given the benefit of the doubt, and laws were uniform across Virginia.”

Schoeneman said that Fairfax reviewed and adjudicated a total of 489 provisional ballots and that of those, the board accepted 271 and rejected 218. He estimated that in about 100 of those cases, the voters came in after Friday — not enough votes to swing the election for Obenshain.

Schoeneman also said that the state’s voter ID law clearly sets a noon Friday deadline for voters who came to the polls without identification to provide it to the board. But for those who voted provisionally for other reasons, he said, “there’s no deadline in the code for when they can or can’t come in.”

He said it was the board’s practice — in this election as in the past — to take input from provisional voters until the board completes its canvass, which it must do within a week of the election. The only difference in this case, he said, was the election was so close that more people than usual came in to make sure their votes were counted.

“[W]e followed the law and SBE guidance as we understood it,” he said in an e-mail. “Despite what some outside observers have claimed, Fairfax’s provisional ballots had no real impact on the final election outcome anywhere in Virginia. By the time we completed our adjudication of all the provisionals, Senator Herring had a 113 vote lead in the AG’s race, the primary race folks are paying attention to. Even if we had rejected every provisional ballot cast in Fairfax — which would have disenfranchised hundreds of qualified voters — Senator Herring’s lead would not have changed.”

In past recounts, Virginia tallied votes only in jurisdictions with obvious problems at the polls. But under a state law passed in 2006, a year after then-Del. Robert F. McDonnell (R) squeaked by Sen. R Creigh Deeds (D-Bath) to become attorney general, votes must be recounted statewide. If a recount is ordered this year, it will involve feeding anew 712,000 paper ballots through optical-scan tabulation machines used in 44 localities, according to estimates from the Virginia Public Access Project. Localities would pick up the tab for the recount, which would probably run into the millions of dollars.

Under a little-known state law, the losing candidate after a recount can contest the results in the General Assembly. In that case, the final determination of the winner would be made by an unusual joint session of the Senate and House of Delegates.

Obenshain has not said whether he would invoke that law, although some Republicans called that highly unlikely unless evidence of election fraud emerged.