The suspect, clad in black, apparently posed for photos with students ahead of the attack, in the western town of Trollhattan.
Two further victims, a pupil and a teacher, are seriously injured. The attacker was shot by police and has died of his injuries. He was 21 and resident in Trollhattan, police said.
The king has said Sweden “is in shock”.
Eyewitnesses described chaos at the school, with hundreds of students fleeing from the building screaming.
Police responded to an emergency call reporting an attack in the cafe area of the Kronan school, at around 10:10 local time (08:10 GMT) on Thursday.
Arriving at the scene, they found a dead man – a teacher – near the entrance.
Two male students and another teacher were rushed to hospital. One of the boys has since died of his injuries.
The pupils’ ages were initially given as 11 and 15, but the hospital has now clarified that they are 17 and 15. It is not clear how old the pupil who has died was.
The attacker was also taken to hospital with gunshot wounds and has since died.
Police have identified the suspect but not released his name. They have searched the suspect’s home and say they have found “interesting” things. He was not previously known to police, spokesman Thord Haraldsson told a news conference.
Asked about possible links to far-right extremism, Mr Haraldsson refused to comment, Swedish radio reports.
Local media have reported that the suspect’s accounts on Facebook and YouTube suggest an interest in Hitler and Nazi Germany. In a photo given to media by a student at the school, he appears to be wearing a black trench coat and a helmet resembling those worn by German troops in World War Two.
Swedish newspaper Aftonbladet published an account (in Swedish) of a girl, who they refer to as Sara, who witnessed the attack.
The girl left her classroom and met two school friends who stood with a masked man holding a bloody knife, she said. He was playing “horrible, Halloween-type” music and did not speak, she said.
Her friends thought it was a joke and wanted to be photographed with the man, so Sara took a photo, Aftonbladet reports.
A teacher came out and asked the man what he was doing, the girl said.
“You’re scaring the children, you can go,” the teacher said.
The man just nodded then stabbed the teacher in the side with his knife, Sara said.
She and her friends ran. The man chased them but they managed to escape.
King Carl Gustav said in a statement that he had learned of the events in Trollhattan “with great dismay and sorrow”.
Sweden’s Prime Minister Stefan Lofven has arrived in the town, and said it was a “black day for Sweden”.
“I think of the victims and their families, students and staff, and the whole of the affected community. No words can describe what they are going through right now. We must ensure that they receive all the support they need,” he said.
Trollhattan is an industrial town in west Sweden, located around 75km (50 miles) north of Gothenburg, the nation’s second largest city.
School attacks are rare in Sweden – with just one incident on record in the past 20 years, in which one pupil was shot dead.