A masked man brandishing a sword broke into a school in Sweden, walking from classroom to classroom to stab students as they opened the door, killing two people and seriously wounding two others before being shot by police and later dying in hospital.

Key points: Masked man stabbed students and teacher in Swedish school

Masked man stabbed students and teacher in Swedish school He posed for photos with students before attack

He posed for photos with students before attack One teacher found dead and one boy died in hospital

One teacher found dead and one boy died in hospital 21-year-old assailant shot by police, later died in hospital

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 attack took place in the south-western town of Trollhattan at a school for children aged between six and 15, shocking a nation where such violence is rare.

Media reports said the assailant was wearing a mask, and school children initially thought it was a prank or a Halloween costume.

One teacher was found dead at the school, while a second victim died in hospital several hours later.

Swedish media reported the second victim was a young boy.

"We cannot confirm the second victim's identity yet as the family is in the process of being informed," hospital spokesman Nicklas Claesson said.

Hospital staff had previously confirmed that two boys, aged 11 and 15, were in critical condition with stab wounds, while another teacher who was seriously wounded in the attack was also being operated on.

The assailant, a 21-year-old man from Trollhattan who was shot by police at the scene, later died in hospital. He had no criminal record.

Several media outlets said the masked man had a YouTube account where he posted material glorifying Adolf Hitler and Nazi Germany, and critical of Islam and immigration.

"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."

Police were alerted about the incident at 10:10 am local time and have already identified the attacker.

However, his identity has not been disclosed.

"When we first saw him, we thought it was a joke. He was wearing a mask and black clothes and [carrying] a long sword. Some students wanted to take their picture with him and feel the sword," one unidentified student told news agency TT.

When the man started attacking people, he quickly realised it was not a joke and fled as the assailant went from classroom to classroom looking for victims.

TT reported several knives were used in the attack.

Police said there was "a lot of confusion" at the school, saying there were still pupils and teachers inside more than two hours after the attack.

Swedish police officers give information to people as they secured the area outside the school in Trollhattan. ( AFP: Bjorn Larsson Rosvall/TT News Agency )

'A black day for Sweden': Swedish PM

According to its website, the Kronan school has around 400 pupils, including many newly-arrived immigrants.

Swedish media described it as a "problem school".

It had been criticised by the Swedish Schools Inspectorate for its lax security and on account of a number of disruptions which prevented the pupils from learning.

Teachers had complained to the inspectorate about the school library and cafe being open to the public and creating an insecure environment for the children, TT reported.

Swedish prime minister Stefan Lofven was expected to visit the town later on Thursday, parliament's Twitter account said.

"This is a black day for Sweden," Mr Lofven said in a statement.

"My thoughts go to the victims and their families, the students and staff, and the entire community. No words can describe what they're going through right now."

"Young people have been attacked. It is terribly tragic," police spokesman Thomas Fuxborg told reporters.

Crisis teams were at the school assisting parents and students.

But some parents outside were unhappy at the way the school handled the attack.

"No one from the school called me. I found out what happened from a neighbour when I was carrying out the trash. I got into my car and came here," one father told TT after being reunited with his young daughter.

The girl, who was not identified, said the head teacher had come into her classroom and told them to stay inside and lock the door.

"We didn't know what was happening," she said.

Trollhattan is an industrial town of 57,000 and home to the former Swedish carmaker Saab which filed for bankruptcy in 2012.

School attacks are rare in normally-tranquil Sweden.

A 1961 school shooting in Kungalv, in south-western Sweden, left one person dead and six others injured.

No other mass shootings have occurred since then, though at least one attack has been foiled, in the southern city of Malmo in 2004. Other threats have been issued but not followed through.

Swedish police officers secure the area outside the school following the attack. ( AFP: Stig Hedstom/TT News Agency )

AFP/Reuters