Two others -- another teacher and student -- were wounded in the attack on the Kronan primary and middle school. The injured were reported to be in serious condition at the hospital in Trollhattan, hospital officials said.
"This is a dark day for Sweden," Prime Minister Stefan Lofven said. "My thoughts go out to the victims and their families, pupils and staff, and the entire community that has been affected. There are no words to describe what they are going through right now."
Law enforcement officials said police shot the suspect at the scene and he died later at the hospital.
An unidentified student gave Agence France-Presse a photo purportedly showing a masked man -- allegedly the person who would commit the attack -- posing with students at the school Thursday morning, AFP reported. The photo shows the man wearing a helmet and holding what appears to be a blade.
The student told AFP the photo was taken before the attack; the precise timing of the photo wasn't immediately known.
A student identified as Edona told Sweden's TV4 that students first thought the man, who was wearing a mask and black clothes, was playing a Halloween joke.
He was "walking like a soldier, kind of, with a sword in his hand," the student said.
But when the man began attacking, the students reacted with horror.
"We were pretty shocked, all of us," Edona said.
Police: 'We fired two shots'
Police received an alarm call shortly after 10 a.m. "that a masked man in his 20s had entered the school ... carrying knife-like weapons in his hands, attacking students and adults at the school," police spokesman Thomas Fuxborg said. "We fired two shots, one of which hit and incapacitated the man so that we could arrest him."
Fuxborg said officers found an injured man outside the school and a dead man just inside.
On the second floor, officers found two students who had been wounded in the attack, Fuxborg said.
Police evacuated the school.
One of the motives they are investigating is the "attacker's possible links to right-wing organizations," according to police spokeswoman Jenny Widen.
She said the two people killed were both men. One was born in 1995; the other in 1998. The attacker was born in 1994, she said.
'Tracks of blood'
Thord Haraldsson, police superintendent in Vastra Gotaland County, told reporters the assailant first attacked a teacher outside the school.
"Then he enters the building and meets another one that he attacks," Haraldsson said. "... Then he systematically searches the school building. We can follow tracks of blood in the premises.
"He walks to the classrooms, knocks on the door, and when (a) pupil opens the door, he is attacked ... stabbed at the spot.
"This happens in two different classrooms, and one of (the attacked students) is now deceased."
Trollhattan is about 420 kilometers (260 miles) west-southwest of Stockholm, the Swedish capital.