Teachers told the children in a classroom at the Professor Henrique Foreis public school to lie down on the floor but some started running away in panic when the shooting started. The school has around 300 pupils aged between 6 to 14.
"They were desperate. Everyone wanted to hide," one student said.
Police said the gunmen shot at officers who had stopped a motorcyclist during an operation to recover stolen vehicles. A state security official said that the police did not fire back at the gunmen.
Eight children were taken to a nearby hospital, including five who were hit by stray bullets and required surgery. Nine more children were hurt by splinters and flying debris.
Violence is widespread in Rio de Janeiro a city of 6 million people, where many of its slums are controlled by drug gangs who frequently battle police and each another.
