Credit…Jes Aznar for The New York Times

MANILA — Election violence erupted in the Philippines over the weekend and on Monday after a shooting between two groups linked to rivals for mayor left four people dead, and a grenade attack wounded nine others.

The shooting occurred on Sunday in the northern province of Ilocos Sur. Separately, local police in the southern town of Maguindanao said five rounds of grenades were fired in a municipal hall, prompting an exchange of gunfire with the police. In Lanao del Sur, videos on social media showed people storming a voting center to destroy ballots and machines. An election official said the government was investigating the episode.

Violence is common during elections in the Philippines, where the government has deployed 270,000 police and military personnel on Monday to thwart such attacks.

Tight security was apparent at elementary schools converted into polling stations, and there were reports of broken voting machines and of some voters having difficulty locating their names on voter registration rolls. At a news conference, Marlon Casquejo, an election official, said the government had counted 143 defective machines across the country. He said these were mostly “isolated incidents,” and blamed old equipment for the problem.

Later in the day, George Garcia, the election commissioner, said more than 1,800 voting machines had malfunctioned and that there were 1,100 backup machines nationwide.

Analysts and election observers have described the race between Ferdinand Marcos Jr. and Leni Robredo, the vice president, as an existential battle for the soul of the country, with consequences that could not be overstated.

Chester Cabalza, the founder of the Manila-based research institute International Development and Security Cooperation, said that voting was not only about the next president, but about “choosing between good governance in a transparent government or a continuity of leadership tainted with lies and revised history.”

Carl Merencillo, a voter in Manila who works at a construction firm, brought his wife and two young daughters to Ms. Robredo’s last campaign rally in Manila’s financial district on Saturday. By midmorning on Monday, he cast his vote for “hope,” he said.

“Definitely this was for the kids. This was one way, really, for me to ensure that the future will be brighter for the kids and their generation,” Mr. Merencillo said.

It took between 45 minutes and an hour for voters to cast their ballots in one precinct outside Manila, as the line snaked about a mile under the searing tropical sun. Officials tried to enforce social distancing rules to prevent the spread of Covid-19, but voters were packed cheek by jowl at many polling places.

Apart from the top job, thousands of local officials, town mayors and senators are also up for election in the Philippines. There are more than 65 million registered voters in the country — a record — and election officers said that polling stations would be open until 7 p.m.