DUBONA, Serbia — The police in Serbia arrested a suspect early Friday after an overnight hunt for a gunman who killed eight people in a rural area near Belgrade, as the Balkan nation, which has one of the world’s highest rates of gun ownership but where gun violence is rare, struggled to come to terms with its second mass shooting in two days.

The attack late Thursday, in which at least 14 others were wounded, came a day after a seventh grader armed with pistols and Molotov cocktails killed eight students and a security guard at his school in Belgrade, Serbia’s capital. An official three-day mourning period for the earlier shooting was to begin Friday.

The suspect in the second shooting was arrested near the city of Kragujevac, about 40 miles south of where the attacks began, and which later continued through a string of suburbs to the south of Belgrade, according to Serbian officials.

Hundreds of police officers had gone door to door in the search for a 21-year-old male suspect, according to RTS, Serbia’s public broadcaster. They deployed helicopters and surrounded the area where they believed he was hiding before he was arrested, the report said.

The shootings took place around 11 p.m. local time on Thursday, Serbia’s Interior Ministry told CNN. It was not clear how long the shootings lasted or exactly where they had begun. The gunman, who was alone, fired shots from an automatic rifle from a moving vehicle, and then fled, according to RTS.

He opened fire ‌near a schoolyard in the village of Dubona, killing a police officer and his sister, in addition to ‌one other person there, according to several local residents. He also shot dead five people on the outskirts of the neighboring village of Malo Orasje, according to relatives of the victims. A senior Serbian police official, who requested anonymity because he was not authorized to speak to the news media, confirmed that the gunman had perpetrated attacks in both villages.

In Dubona, a small village where vineyards extend through hilly landscapes, Zlatko Vujic said his 25-year-old nephew was among those shot dead by the gunman. Mr. Vujic said the suspect, whom he knew, had been working at a nearby fruit farm.

“He was just a kid,” he said, his voice quavering, referring to the suspect. He said the gunman’s father had served in the Balkan wars that led to the breakup of the former Yugoslavia in the 1990s.

President Aleksandar Vucic of Serbia said on Friday that the gunman’s father is a deputy colonel in the Serbian Army.

On Friday morning, police officers and army members surrounded the house of the gunman’s family in the tiny village of Donja Dubona, near the village of Dubona.

Stefan Markovic, 29, a resident of the village who said he had known the gunman since they were children, said his father had kept numerous weapons in the house. He described a family wedding he attended a few years ago in which the suspect’s father and other family members shot rifles into the air to celebrate.

On Friday morning, large blood stains were still visible on the road in Dubona where the gunman had shot two people. “It’s a shock,” said Javorka Pavlovic, a resident of the village, as she stood close to where one of the shootings took place. Ms. Pavlovic said she heard the gunshots late in the night but thought that they were fireworks.

The villages where the attack took place are sparsely populated suburbs on the southern edge of Belgrade, near the slopes of Mount Kosmaj. After initially searching in darkness with thermal imaging cameras, the police began a physical search as dawn broke, RTS reported.

Forensic teams in white suits were at work throughout the area. In Dubona, officials put crime scene labels near a bench in front of a school where a young man was reportedly killed. On a road between Dubona and Malo Orasje, a nearby village where the gunman shot several people dead, forensic scientists used a metal detector to search the area, apparently looking for bullets.

Serbia’s interior minister, Bratislav Gasic, called the shooting “a terrorist act,” RTS reported.

A day before Thursday’s attack, a seventh-grade student armed with pistols and Molotov cocktails fatally ​shot eight other children and a security guard at his school in Belgrade, plunging the capital into grief and stunning the nation.

After that shooting, the Serbian government on Thursday approved a series of measures to tighten gun regulations, including setting a two-year moratorium on new licenses and enhanced surveillance of shooting ranges. The changes followed suggestions by Mr. Vucic. On Wednesday, he urged the government to address the roots of the violence.

The Interior Ministry urged gun owners to ensure their weapons were locked away, unloaded and separated from ammunition. The ministry said it would go through the registry of gun owners to check that arms were properly stored and seize weapons or take other actions against owners if they were not.

Serbia has historically had a high level of gun ownership compared with other countries — because of its recent history of armed conflict and a cultural tradition of owning guns — but has not had high levels of gun violence, according to an October 2022 report by the Flemish Peace Institute, an independent research group.

From 2015 through 2019, 125 people were killed in firearm-related homicides in Serbia, a country of about seven million people, according to the report. According to the 2018 Small Arms Survey, Serbia ranks third in the world, after the United States and Yemen in civilian firearm ownership, with an estimated 39 firearms per 100 people.

Serbia has enacted stringent regulations on firearms since guns became widely available as a result of the Balkan wars of the 1990s. Gun owners must have no history of imprisonment and have no criminal record in the past four years, be trained in handling firearms, undergo routine medical examinations, and have a safe storage space.

Serbia’s last mass shooting occurred in 2016, when a man killed five people at a cafe in the country’s north. In 2015, a man killed four people after his son’s wedding, including his wife, his new daughter-in-law and her parents.

In 2013, a 60-year-old veteran of the Balkan wars killed 13 people, including relatives and neighbors, in the village of Velika Ivanca near Belgrade. And in July 2007, a 38-year-old man killed nine people who had been passing by on a street in the village of Jabukovac in eastern Serbia.

This is a developing story. It will be updated.

Constant Méheutreported from Dubona, and Victoria Kim, Matej Leskovsek and John Yoon reported from Seoul. Alisa Dogramadzieva contributed reporting from Dubona, and Jenny Gross from London.