Sweden was shocked by Thursday's attack in which the assailant walked through a school stabbing students and staff in Trollhattan, an industrial town of about 50,000 inhabitants in western Sweden that has a large proportion of immigrants.
Police shot the suspect, a local man in his early 20s, who died of his injuries at a hospital. He had no criminal record.
"We are convinced that the assailant was driven by racist motives when he carried out the act," police chief Niclas Hallgren told Swedish public service radio.
"We have reached this conclusion based on what we found when we searched his apartment and his behaviour during the act, and also on the basis of how he selected his victims."
Sweden in 'shock'
King Carl XVI Gustav said Sweden was "in shock" following the attack, and that the royal family had received the news with "great dismay and sadness".
Local media showed what it said was a picture of the assailant carrying a sword and dressed in a black trench coat and helmet, posing for pictures with students shortly before the attack.
"We thought it was a joke, a Halloween prank or something, but it wasn't," one witness student told TV4.
The victims have been named by Swedish daily Expressen as 17-year-old student Ahmed Hassan and 20-year-old teaching assistant Lavin Eskandar.
Ahmed Hassan reportedly came to Sweden from Somalia three years ago with his family.
"I ran to the school, but I couldn't find Ahmed anywhwere. The police told me to go to the hospital and I was so scared," Ahmed's mother Kiin Mohamed Ali told Expressen.
Lavin Eskandar's brother, Leith Eskandar, said his brother was trying to protect the children.
"All I know is that he protected others and not himself, and that he tried to protect the kids. He was the only one who managed to stop the attacker," he told Expressen.
The attacker has been named by Swedish media as Anton Lundin Pettersson, a 21-year-old man who according to reports was "quiet and withdrawn." Police have yet to confirm the identity of the suspected killer.
Local media said the suspect's social media accounts showed extreme right-wing tendencies.
Police would not give any details of his motive but said possible far-right sympathies were being looked into, as part of a broader investigation that was being assisted by Sweden's security service.
The Kronan school is in Trollhattan, an industrial town of about 50,000 inhabitants in western Sweden that has a large proportion of immigrants and has been plagued by high unemployment after the demise of car company Saab which was headquartered there.

A crowd gathers near the scene of a sword attack by a masked man at the Kronan school in Trollhattan, Sweden, Thursday Oct. 22, 2015. (Bjorn Larsson Rosvall / TT via AP) Source: AAP
"This is a black day for Sweden"
Attacks in schools are rare in Sweden, with the last similar attack taking place near Gothenburg in 1961.
"This is a black day for Sweden," Prime Minister Stefan Lofven said in a statement.
Police arrived minutes after the attack, following trails of blood smeared across the school corridors to find the assailant.
Police said they had responded to an emergency call saying a masked man with a sword was on the premises and that a person had been attacked at a cafeteria.
They shot the suspect, in his early 20s, who died later of his injuries at a hospital. He had no criminal record.
Photos by a local news agency showed several ambulances and police cars on the scene as the emergency services dealt with distraught adults and teenagers. A police cordon was marked out with white tape.

Prime Minister Stefan Lofven, right, talks to local people near the school Thursday afternoon where a masked man wielding what looked like a sword stabbed four people Thursday Oct. 22, 2015, in Trollhattan, Southern Sweden. Source: AAP
The lower grades of the school were criticized this year by Sweden's education watchdog for failing to address problems in providing a safe and calm environment for students and staff, saying teachers struggled to conduct teaching in some classes.


People and parents leaving school after attack at a primary and middle school in Trollhattan, southwestern Sweden, on October 22, 2015, where a masked man armed with a sword attacked students and staff members. (STR/AFP/Getty Images) Source: Getty Images

People stand next to candles at a makeshift memorial site at a school in Trollhattan, southwestern Sweden, on October 22, 2015 (Photo credit should read JONATHAN NACKSTRAND/AFP/Getty Images) Source: AAP