The man who stabbed two people to death at a school in Sweden before being shot and killed by police had a racist motive and had planned the attack.

Police labelled the stabbing in the industrial town of Trollhattan as a hate crime based on discoveries they made when searching the man's home, the way he dressed, his behaviour at the scene and the way he selected his victims.

"All together, this gives a picture that the perpetrator had a racist motive when he committed the crimes at Kronan school," police said in a statement.

The 21-year-old masked man, Anton Lundin Pettersson, entered the school in southern Sweden on Thursday and stabbed four people, two of whom died, before he was shot by police. He later died of his wounds.

Senior police officer Niclas Hallgren told a news conference surveillance videos show the attacker roaming inside the school with a sword and a sharp knife.

Evidence shows he acted alone and police found "a kind of suicide note" in his apartment after the stabbing, investigator Thord Haraldsson added.

Police also found the sword's holster inside a car parked nearby the school that the attacker could have used.

The victims were 20-year-old Lavin Eskandar, a mentor at the school, and Ahmed Hassan, a 15-year-old student. Both died of their stabbing wounds.

The attack stunned Sweden, where violent crime is relatively rare, though there has been a recent spate of arson attacks on asylum centres in the south of the country as the influx of migrants has surged. Immigration officials estimate that some 190,000 will arrive this year, second only to Germany in western Europe.

"This is a black day for Sweden," Prime Minister Stefan Lofven said of the attack. "It is a tragedy that hits the entire country."

Police confirm surveillance videos show the assailant posed for photos with students before beginning his deadly rampage in the halls of the Kronan school in Trollhattan, a city of 56,000 people.

By the end of 2013, 10,373 people in Trollhattan were foreign-born, according to official figures, with most coming from the Middle East, the Balkans and Ethiopia. A majority of the students at Kronan are foreign-born.

Many students thought the attacker was playing a Halloween prank. News outlets posted a photo of a black-clad man they identified as the attacker wearing a military-style helmet and a black mask with a sword in his hand.