A 23-year-old former student returned to his public elementary school Thursday with two guns and began firing, killing at least 12 children as he shouted, "I'm going to kill you all!"
The man then fatally shot himself in the head after being wounded by police.
It was the worst school shooting in Brazil — and would have been deadlier if the gunman had not been shot in the leg by a police officer.
Eleven girls and one boy were killed, and at least 11 children were wounded, the O Globo news website in Rio de Janeiro reported.
Images taken with a cell phone and posted on YouTube showed students fleeing wildly, screaming for help, many with their white and blue school shirts soaked in blood.
"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 neighborhood 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."
Marcos Silva, 11, who was in the school but was not harmed, said the experience was "like a horror movie."
"Everyone lay down on the ground in silence, the teacher asked us not to make noise so the killer wouldn't notice us," Silva said. "I thought to myself, 'If he comes in, we're all going to die.'"
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.
Brazilian President Dilma Rousseff, attending an event in the capital, Brasilia, lamented the deaths of "defenseless 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."
Authorities closed the school temporarily while they investigated, but Paes said it would reopen.
Terrified parents earlier rushed to the school in the Realengo neighborhood. Television images showed them crying and screaming for information about their children.
Authorities closed the school temporarily while they investigate, but Paes said it would reopen.
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?"