A masked man wielding "knife-like weapons" walked from classroom to classroom to stab students as they opened the door, killing a teacher and a boy and wounding two others at a school in western Sweden before being shot dead by police.
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 suspect, in his early 20s, died later of his injuries at a hospital. He had no criminal record and the motive for the attack was not known.
Police arrived minutes after the attack, following blood trails smeared across the school corridors to find the assailant.
The incident occurred at the Kronan school in Trollhattan, an industrial town of about 50,000 inhabitants north of Gothenburg. The town has been plagued by high unemployment after the demise of car company Saab which was headquartered there.
"This is a black day for Sweden," Prime Minister Stefan Lofven said in a statement.
Photos by a local news agency showed several ambulances and police cars on the scene as emergency service staffers dealt with distraught adults and teenagers. A police cordon was marked out with white tape.
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.
Attacks in schools are rare in Sweden, with the last similar attack taking place near Gothenburg in 1961.
The lower grades of the school were criticised 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.