The 29-year-old man attacked three male students and one female parent with a vegetable knife around 11:30 a.m. (0330 GMT), the Xuhui branch of the Shanghai police said on its official Weibo account.
The victims were rushed to hospital but two of the students died, it said, adding that the third student and the parent were not in a life-threatening condition.
The man, who was arrested at the scene, was unemployed and angry at society, the police said. A video circulated on Chinese social media platform Weibo showed people tying up the attacker with rope on a roadside pavement.
Violent crime is rare in China compared with many other countries, especially in major cities where security is tight, but there has been a series of knife and axe attacks in recent years, many targeting children.
In April, a 28-year-old man who harbored a hatred of children having been bullied at school stabbed to death seven Chinese middle school students who were on their way from classes in the northwestern province of Shaanxi.
Such attacks are often blamed on people with mental illness or who have personal grievances. Knives are most commonly used because gun controls are extremely strict in China.
More about: #Shangai