The state government of Sao Paulo said two teenagers, armed with guns and wearing hoods, entered the school and began shooting at students.
Besides the five students, the dead included a teacher and a school administrator, said Joao Camilo Pires de Campos, the state's public secretary.
Even though Brazil is one of the most violent countries in the world, it is rare to see school shootings in the South American country.
Brazil's new President Jair Bolsonaro recently announced gun ownership controls would be loosened. "I opened the gate to my house and I saw children jumping over the school's wall".
The last major school shooting in Brazil occurred in 2011, when 12 students in Rio de Janeiro were shot dead by a gunman.
Gun laws are strict in Brazil, but it is not hard to purchase a weapon illegally.
At least nine people were killed at a Brazilian elementary school where two young men were seen entering the building and firing weapons early on Wednesday, according to Sao Paulo police.
Another 17 people - mostly school kids - were shot and injured, and several of them were in serious condition, said police, who were not yet aware of a motive for the violence.
"I heard a lot of screaming", she said.
Aged between 20 and 25 years old, the alleged attackers are not believed by police to be former students of the school that has some 1,000 primary and secondary students.
"We can't let those who take advantage of this tragedy speak about how disarmament is the solution", he tweeted, adding: "Weak and shameful 'disarmament farce, ' which gave guns to criminals and prevented self-defence". Police said they also found a bow and arrow as well as items that appeared to be explosives.
He said it was not yet known whether the assailants were former pupils at the school.