Police chief Niclas Hallgren said they had based their conclusion on what was found at the killer's apartment and "his behaviour during the act".
Media reports suggest the 21-year-old attacker had far-right sympathies.
Armed with a sword and wearing a helmet and mask, he stormed a school in Trollhattan, near Gothenburg, before being shot dead by police.
"We can confirm that this was a racially motivated hate crime partially because the man chose his victims based on the colour of their skin," Mr Hallgren told Swedish Radio.
The helmet he wore was similar to German World War Two soldiers' helmets.
He was seen posing for a photograph with students, who thought he was dressed for Halloween, before going from classroom to classroom at the Kronan School.
A teacher and 17-year-old boy died from stab wounds, while another male student, 15, and 41-year-old teacher remain in serious condition in hospital.
The attacker's name has not yet been released by police, though they say he was local to Trollhattan. He did not have a criminal record, police say.
The Swedish daily Expressen and other Swedish media named him as Anton Lundin Pettersson, aged 21.
He had allegedly joined a campaign to push for a referendum on whether Sweden should continue accepting migrants.
Sweden has reacted with shock to the killings. Before visiting the scene of the attack, Swedish Prime Minister Stefan Lofven said it was a "black day" for the country.
"School is supposed to be the place for learning, play and curiosity and friendship and therefore this is a tragedy that affects the whole country," Mr Lofven told reporters.
King Carl Gustaf said he was "in shock" and that he had learned of the events in Trollhattan "with great dismay and sorrow".
Dozens of people gathered outside the school building on Thursday evening to pay their respects to the victims and place flowers and candles on the ground.
Some residents of the town who were holding a vigil held up posters that read "why kill?"
Swedish media reports say the suspect's accounts on Facebook and YouTube suggest he had an interest in Hitler and Nazi Germany, as well as hostility to Islam and immigration.
"He was a loner. He played video games, lived in his own world," a former classmate told Expressen.
Eyewitnesses described scenes of chaos, with the attacker knocking on the doors of at least two classrooms and attacking two male students who opened them. One of the boys later succumbed to his injuries.
None of the victims have been identified by police.
Laith Alazze, 14, told Sweden's TV4 that one of his friends walked over to the assailant to challenge him "but when we saw he stabbed [the teacher], we ran away''.
Another student told the same station that the attacker, who along with the mask was clad in black, "walked sort of like a soldier with a sword in his hand".
Police were alerted to the attack at around 10:10 local time (08:10 GMT) on Thursday, and later gunned down the attacker in the hallway outside a classroom.
The Kronan school has about 400 students aged between six and 15, including many children of immigrants.
Trollhattan is an industrial town in west Sweden, located about 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.