A motive was not yet clear.
Besides the five students, the dead included a teacher and a school administrator, said Joao Camilo Pires de Campos, the state's public secretary. "The teenagers were brutally murdered". At the time of the shooting, reportedly, only secondary students were at the school.
"This is the saddest day of my life", de Campos said, speaking to reporters outside the school in the Sao Paulo suburb of Suzano.More news: Travel advisory issued for Japan over measles outbreak
The assailants were trying to force their way inside a room at the back of the school where many students were hiding when police arrived.
The attackers appeared to be in their early to mid-20s, and authorities don't believe they were former students, the governor said.
The public school, Raul Brasil Professor, has more than 1,600 students from elementary to high school grades, teachers gathered outside said. Before Wednesday, the last school shooting in Brazil occurred in 2011 when 12 children were killed by a former student in a Rio de Janeiro school.
Before entering the Raul Brasil school in Suzano near Sao Paulo, the former pupils aged 17 and 25 shot and killed the younger assailant's uncle, who owned a auto rental agency where they stole a vehicle.More news: Verizon launches 5G in Chicago, Minneapolis at $10 extra cost
Brazil is one of the most violent countries in the world with 64,000 murders in 2017, a rate of nearly 31 per 100,000 inhabitants - that's three times higher than the level the United Nations classifies as endemic violence.
Gun crime is extremely common in Brazil but school shootings are rare.
Far-right President Jair Bolsonaro, who took office in January, has signed a decree making it easier for law-abiding citizens to own a gun, a key campaign promise, even though many restrictions remain in place.More news: At age 30, World Wide Web is 'not the web we wanted'