09:30 local time (12:30 GMT) when the students were on a break at the state school in Suzano, near São Paulo.
The gunmen wearing face masks, aged 17 and 25, killed themselves after the attack, according to police.
While gun crime is common in Brazil, shootings of this nature are not.
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.
The motive for the shooting is not clear. Police say they found a revolver, a bow and arrow and items that appeared to be explosives.
Why are there so many murders in Brazil?
The school has some 1,000 primary and secondary students, aged between six and 18, and a language centre. It is located in downtown Suzano, some 50km (31 miles) from São Paulo.
The gunmen entered the school and shot dead the co-ordinator, 59-year-old Marilena Ferreira Umezo, before killing another employee, said Marcelo Salles, commander of police forces in São Paulo.
They then killed four students and injured several others before going to the language centre, where a teacher and students locked themselves in a classroom. The attackers then killed themselves in the hallway.
Only secondary students were at the school at the time of the shooting. Police arrived eight minutes after being called and did not confront the gunmen. Two students later died in hospital.
"I was in the classroom during our break. I thought [the sound] was from bombs. When I realized they were gunshots, I stayed there. I only left when the police arrived," teacher Sandra Perez told O Estado de S.Paulo newspaper (in Portuguese).
"It's a very sad scene, the saddest thing I've seen in my entire life. The teenagers were brutally murdered," São Paulo state governor João Doria said after visiting the school.
A man was injured in another shooting near the school earlier, but police say they are still investigating if both cases are connected.
The last major school shooting in Brazil happened in 2011, when 12 students were shot dead by a gunman in Rio de Janeiro.