RIO DE JANEIRO—A gunman roamed 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 if the gunman had 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.

Rio de Janeiro state's Secretariat of Health and Civil Defence said in a statement on its website that 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 injured.

The dead included 10 girls and two boys, plus the gunman, according to the Health and Civil Defence department. Those killed were between the ages of 12 and 15. One of the boys died at a hospital about 12 hours after the shooting.

“He came in shooting,” said Andreia Machado, recounting what her 13-year-old daughter, Thayane, told her before going into surgery.

Thayane was hit by three bullets and lost feeling in her legs because one hit her spine, said her mother. Crying as she spoke, Machado wondered if 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.”

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

No motive was known, but authorities said the shooter left a rambling and 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. The 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 the AIDS virus.

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.

Rio Police Chief Martha Rocha said that when Oliveira first entered the school he told staff members he was there to give a lecture.

Shortly afterward, he opened fire. Rocha said he was carrying two pistols and an ammunition belt. He fired off at least 30 rounds.

Rio is a city rife with drug-gang violence in its vast slums, but school shootings are rare. The gunman had no criminal history, Rocha told a news conference.

About 400 people were inside the school when the shooting began about 8:30 a.m. local time. The school serves grades one through eight.

“What happened in Rio is without a doubt the worst incident of its kind to have taken place in Brazil,” said Guaracy Mingardi, a crime and public safety expert at the University of Sao Paulo.

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.

Police were alerted to the shooting when two young boys, at least one with a gunshot wound, ran up to two officers on patrol about two blocks away. The officers sprinted to the school and at least one quickly located the gunman on the second floor and traded shots with him.

“He saw me and aimed a gun at me,” said officer Marcio Alves. “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-story, pastel yellow and green school was turned into a “hellish nightmare.”

“This day would have been so much worse if it weren't for the hero policeman,” Paes told reporters at the school.

Authorities closed the school temporarily while they investigate, but Paes said it would reopen.

Rio Gov. Sergio Cabral called the shooter a “psychopath” and said there were no indications anyone else was involved in the shooting, but that the investigation would continue.

“We have to investigate where he got the weapons and where he learned to use them,” the governor said.

Terrified parents rushed to the school in the Realengo neighbourhood. 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. “People were asking for help, but what could I do?”

Three helicopters were landing and taking off from a football field next to the school, ferrying the wounded to hospitals.

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.”