Voters in the District, Virginia and Maryland inundated polling places Tuesday and spent hours inching forward to cast ballots in tightly contested national and local races as strong voter turnout caught election officials by surprise.

Hundreds of people throughout Virginia were still in line nearly three hours after polls were scheduled to close, including in Prince William and Fairfax counties. Election officials said voting could continue for hours until everyone in line by 7 p.m. had voted, “regardless of how long it takes,” said Nikki Sheridan, a spokeswoman for the state board of elections.

At some polling places in Northern Virginia, lines were so long that campaign volunteers handed out water bottles, granola bars and coffee to fortify people for a wait of more than three hours.

Although voters waited patiently in most locations, some did U-turns, vowing to return later rather than brave hours standing outside in the November chill.

“It was a big mess. I’m just so depressed, because I saw so many Obama voters leaving, and you know how tight it is in Virginia,” said Fred Crouch, an Obama campaign volunteer who handed out provisions to Crystal City voters.

Exit polls 2012: How votes are shifting

Throughout the day, election officials reported other problems: a temporary shortage of provisional ballots in one District polling place, technical glitches in some Maryland lo­cations and some confusion about new voter-identification requirements in Virginia. But by far, the biggest headache for voters was the wait to vote, which election officials attributed to overwhelming interest in high-profile contests and to lengthy ballots.

Election officials said they were unprepared for the crush and tried to redeploy workers to polling locations with the biggest backups. In the District, the midday breakdown of the one electronic voting machine at Oyster Elementary School in Woodley Park forced some voters who had already waited for two hours to move to the back of another line, with a wait of more than an hour, and instead vote by paper ballot.

In Maryland, it took poll workers longer than usual to process voters at one Bowie precinct because of a shortage of voter access cards.

In Fairfax County, a fifth voting machine added midday at the Skyline precinct had little effect on the long wait in the stuffy hallways of the Goodwin House retirement facility.

Throughout Virginia, there were some reports of confusion about the new ID requirements. Some poll workers apparently did not realize that the new law does not require a photo ID.

Melinda Schweihs of Alexandria said a poll worker turned down her government-issued voter card. Schweihs had a photo ID with her, but she said in an e-mail that she was distressed for other voters.

“How many people are they demanding this of, and turning away?” she wrote.

Alexandria’s elections chief, Tom Parkins, said that such complaints were rare Tuesday. He said poll workers should have known from training that photo identification was not required.

Just before 10:30 p.m., the doors to Prince William County’s Potomac Middle School auditorium shut, with the last 30 voters in line to cast ballots. Annie Sasjous had waited for four hours and 10 minutes to cast her vote for Obama.

“I have not waited four hours for anything — traffic, nothing,” Sasjous said. “People in the line were saying, ‘Press on, press on.’ ”

T. Rees Shapiro, Laura Vozzella, Rosalind S. Helderman and Lynh Bui contributed to this report.