At first, students thought it was a Halloween prank.

The man walked into the Kronan school in Trollhättan, Sweden, on Thursday morning dressed in the style of Darth Vader, with a long black trench coat, black boots, black helmet and a mask similar to the “Star Wars” villain’s. He played eerie music but didn’t say a word.

Strangest of all, he carried a sword.

Several students walked up to the man and snapped smiling photos on their cellphones.

“When we first saw him we thought it was a joke,” a 14-year-old student named Laith told Swedish state broadcaster SVT. “There were pupils who wanted to take pictures with him and feel the sword.”

What seemed like a Halloween joke, however, quickly turned into genuine horror. The man’s sword became a deadly weapon that was wielded with a  “racist motive,” said police in a nation the prides itself on tolerance, but has been stunned from a series of suspicious fires at sites set aside to house a flood of asylum seekers and other migrants.

“Everything points to this being a hate crime,” the lead investigator, Thord Haraldsson, told journalists Friday.

“He selected his victims and attacked the dark-skinned ones and left the light-skinned ones alone,” he said.

The bloodshed began when a teacher’s assistant told the unannounced visitor to leave because he was scaring the students, and the Darth Vader-esque impersonator suddenly stabbed the 20-year-old man.

Then the man turned his weapon on the students, stabbing two teenagers before attacking another teacher, according to police.

A masked man carrying a sword killed a teacher and a student, and seriously wounded two others, at a school in Sweden in the first such attack since 1961. (Reuters)

“He hunted us through the school,” another student told SVT.

The bizarre attack ended when police arrived at the school and fatally shot the intruder.

The young teacher, Lavin Eskandar, died at the scene while a student, Ahmed Hassan, later died at a hospital, police said. Two others, a student and a 41-year-old teacher, were injured.

Police described the attack as linked to racism and Nazi-style sympathies, based on evidence including a suicide note.

“All together, this gives a picture that the perpetrator had a racist motive,” police said in a statement.

The attacker’s choice of school may not have been random. Kronan has a high proportion of immigrants in a town of about 50,000 in which nearly one in four people are immigrants or whose family came to Sweden in recent generations.

Based upon his Nazi-like helmet, his manner of selecting his victims and material discovered in his Trollhattan home, the 21-year-old swordsman was motivated by racial hatred, police said.

“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 obtained by the AP.

The apparently racially motivated killing spree was the latest jolt for Sweden, a country that has long prided itself on civility and social cohesion but has recently seen an influx of immigrants as well as a surge in far-right, anti-immigrant sentiment. The attack is believed to be the deadliest ever to target a Swedish school.

“This is a black day for Sweden,” Prime Minister Stefan Löfven said Thursday. “It is a tragedy that hits the entire country.”

Several Swedish media named the alleged attacker Friday, but his identity has not been confirmed yet by authorities. Social media accounts allegedly linked to the suspect show he liked German metal bands and videos about Nazi weaponry.

The attack caused panic in Trollhättan, an industrial city in western Sweden near Gothenburg. Terrified students, teachers and parents gathered outside the school on Thursday morning, according to the Guardian. Many returned after nightfall to light candles for the four victims.

The anti-racist organization Expo said it knew the identity of the attacker and claimed he “showed clear sympathies with the extreme right and anti-immigration movements,” according to the Guardian.

Police said the attacker entered the school through a cafe lobby that is open to the public before knocking on classroom doors. Eventually, the teacher’s assistant opened a door to tell the masked man to leave.

“You are scaring the children, get away from here,” the teacher said, according to a student named Sara.

The masked man “nodded, then he plunged the knife into the teacher’s right side,” Sara told SVT. “The last thing I heard him say was, ‘Call an ambulance.'”

The swordsman then stalked students through the school, stabbing two of them and the second teacher.

“He was playing strange music and didn’t say a word,” Sara told SVT. “It was terrifying music, sort of Halloween music.”

Another teenage student recounted his near-death experience to the English-language paper, The Local.

“I was in a classroom with my class when one of my classmates’ sisters called her to warn her that there was a murderer at the school,” the unidentified male student said. “So we locked the door to the classroom, but our teacher was still outside in the corridor.

“We wanted to warn him, so a few of us went outside and then I saw the murderer; he was wearing a mask and had a sword. Our teacher got stabbed.

“The murderer started chasing me. I ran into another classroom,” he said. “If I had not run, I would have been murdered. I’m feeling really scared. Everyone’s scared here.”

A news photographer who arrived at the scene described “complete chaos,” including an ambulance that had crashed into a school wall, according to the New York Times.

Maria Randsalu, a police spokeswoman, told the newspaper that the suspect had no criminal record.

Prime Minister Löfven urged his fellow Swedes to unite following the tragedy.

“What must never happen, has happened here today,” he said at a news conference, The Local reported.

“We’re going to embrace those we love [tonight] and think of those who no longer are able to do that,” he said. “Take care of each other. Together we take care of Sweden.”

Sweden — with its generous social welfare programs — is among the main destinations for the huge tide of refugees and others pouring into Europe from places such as war-wracked Syria. Earlier this week, the U.N. refugee agency said more than 500,000 people have crossed this year into Europe over the often-perilous seas between Turkey and Greece, and the numbers are expected to further rise before winter sets in.

In the past week, at least three fires have damaged buildings used to shelter refugees and others.

“We will have to ask ourselves several questions about how society is developing, about polarization and mobilize all good forces against this racist violence,” Interior Minister Anders Ygeman told Sweden’s TV4.

Editor’s Note: An earlier version of this story said the attacker wore a Darth Vader mask. The mask was merely in the style of the “Star Wars” character.

More from Morning Mix: