A gunman prowled the halls of an elementary school in Rio de Janeiro on Thursday and killed 12 children, lining them up against a wall and shooting them in the head at point-blank range as he shouted, "I'm going to kill you all!"

It was the worst school shooting in Brazil, and would have been deadlier had the gunman not been shot in the legs by a police officer, who said the man then fell down some stairs and shot himself in the head.

Images taken with a cellphone and posted on YouTube showed students fleeing wildly, screaming for help, many with their white and blue school shirts soaked in blood.

Killer leaves police burial instructions

Brazilian authorities say they have no idea what turned Wellington Oliveira into a mass killer, but the shooter did leave a mostly incoherent letter at the scene indicating he wanted to kill himself.

The letter also explained in detail how Oliveira wanted his corpse to be cared for: bathed and wrapped in a white sheet that he left in a bag in the first room where he said he would start shooting. Oliveira's letter also states that the gunman should not be touched by anyone who is "impure" unless they wear gloves.

"If possible I want to be buried next to my mother. A follower of God must visit my grave at least once. He must pray before my grave and ask God to forgive me for what I have done," read the letter, portions of which were posted on the Globo television network's website.

Edmar Peixoto, the deputy mayor of western Rio, said the letter also stated the gunman was infected with HIV.

Rio de Janeiro state's Secretariat of Health and Civil Defence said in a statement on its website that the dead included 10 girls and two boys, plus the gunman. Those killed were between the ages of 12 and 15. One of the boys died at a hospital about 12 hours after the shooting.

At least 12 other students were injured, many by gunfire, and taken to hospitals. At least two were in grave condition. Officials earlier reported 18 wounded.

"He came in shooting," said Andreia Machado, recounting what daughter, Thayane, 13, told her before going into surgery.

Thayane was hit by three bullets and lost feeling in her legs because one hit her spine, her mother said. Crying as she spoke, Machado wondered whether her daughter would ever be able to return to school — or walk.

"She's such an active child," she said. "That's the biggest fear I have, her not being able to walk again. But we have to trust in God."

Shooter posed as lecturer

The gunman was identified as Wellington Oliveira, 23, who had once attended the Tasso da Silveira school, which is located in a working-class neighbourhood in western Rio.

The public school was in the midst of celebrating its 40th anniversary, and students' handmade posters commemorating the day could be seen through school windows.

Police Chief Martha Rocha said that when Oliveira entered the school he told staff members he was there to give a lecture. Shortly afterward, he opened fire. Rocha said Oliveira, who had no criminal record, was carrying two pistols and an ammunition belt. He fired off at least 30 rounds.

Two young boys, at least one with a gunshot wound, ran up to two police officers on patrol about two blocks away from the school as the shooting started. The two officers sprinted to the school and quickly located the gunman on the second floor and traded shots with him.

"He saw me and aimed a gun at me," officer Marcio Alves said. "I shot him in the legs, he fell down the stairs and then shot himself in the head."

Rio Mayor Eduardo Paes said life at the four-storey, pastel yellow-and-green school had been turned into a "hellish nightmare. This day would have been so much worse if it weren't for the hero policeman."

Children begged to be saved

About 400 people were inside the school, which serves Grades 1 to 8, when the shooting began about 8:30 a.m. local time.

Jade Ramos, a student at the school, said the gunman repeatedly yelled "I'm going to kill you all!"

"He had already killed a lot of children in the first floor and in the yard," she told the Globo television network. "He kept telling the kids to face the wall and was shooting at their heads. The children kept begging, 'No, please!' There was a lot of blood, children agonizing on the stairs."

Ramos said she escaped by running into a classroom, where a teacher then locked the door and barricaded it with tables.

Terrified parents rushed to the school. Television images showed them crying and screaming for information about their children.

Zilda Nunes, 67, lives across the street from the school and said three of her grandchildren were inside when the shooting began. She screamed for help when she heard the gunfire, but didn't enter the building.

As students fled, she offered them sugar water to help calm them down, she said. "There were so many children shot, so much blood," Nunes said.

Brazilian President Dilma Rousseff, attending an event in the capital, Brasilia, lamented the deaths of "defenceless children."

"I ask for one minute of silence for these children who were taken so early from their life," she said, her voice cracking and eyes welling with tears. "It's not in the nature of our nation to have these types of crimes."