Masovian Regional Governor Mariusz Frankowski told reporters that nine people had been taken to hospital but that none of them was in a life-threatening condition.
"The injured included five students, two teachers and two bystanders as well as the perpetrator, who inflicted self-harm," he said.
Police said the suspect was a student at the school and that he had attacked people with a machete.
Bomb disposal experts were called to the scene.
National Prosecutor Dariusz Korneluk told private broadcaster TVN24 that a homemade explosive device that nearly detonated was found in the attacker's backpack.
The attack took place at a vocational school for teenagers.
Fire brigade spokesman Adrian Stupski told state news channel TVP Info that students had barricaded themselves into classrooms in self-defence before they were rescued.
Police said they detained the suspect about 20 minutes after the attack and posted a photo on X showing a man they appeared to have apprehended lying face down in a wooded area.
The town's mayor Pawel Niewiadomski said the situation was under control.
He announced that psychological support would be provided for the pupils and teachers affected.
Ostroleka has a population of about 48,000.
The vocational training centre has been closed until further notice.
Lessons have also been suspended at all other schools in the town.
with Reuters