A seventh-grade student went on a shooting rampage at a school in Belgrade, Serbia, early on Wednesday, killing eight children and a security guard, the Serbian police said.

The police said in a statement that the shooting took place around 8:40 a.m. at Vladislav Ribnikar primary school in the upscale neighborhood of Vracar. The boy, who was identified only by his initials, K.K., and was born in 2009, used his father’s handgun to fire into a crowd at the school, the police said.

The suspect was apprehended in the schoolyard, the statement said. Six children and a teacher were injured in the attack and taken to the hospital. A security guard was killed while trying to stop the attack, according to the district’s mayor, Milan Nedeljkovic.

“He wanted to prevent a tragedy, which would have been even greater if he had not stood in front of the boy who shot,” Mr. Nedeljkovic said in an interview with state media outside the school on Wednesday.

“The children are under stress. The school is closed. Something like this has never been recorded in the history of Belgrade schools,” he added.

Video broadcast on local TV from the scene of the shooting showed dozens of vehicles, including ambulances and police cars, stationed outside the school, watched by local residents.

Photographs released by news agencies showed the police detaining the student, whose head was covered with a dark piece of clothing. The authorities did not offer any motive for the shooting.

“All police forces are still on the ground working intensively to shed light on all the facts and circumstances that led to this tragedy,” said the statement, which was posted by Serbia’s ministry of public affairs on Facebook. Gun violence is rare in Serbia, although stockpiles of weapons from the Balkan wars in the 1990s remain and many Serbs keep weapons at home for protection.

A mass shooting in 2013 left 13 people dead after a 60-year-old man opened fire in a village near Belgrade.

This is a developing story. Please check back for updates.