BEIJING — A man wielding a knife killed nine children and injured 10 others outside a middle school in central China on Friday, the authorities said, in one of the worst attacks at a Chinese school in recent years.

The episode took place shortly after 6 p.m., officials said, as students were being dismissed at the Mizhi County No. 3 Middle School in Shaanxi Province, about 500 miles southwest of Beijing.

The police said they had taken into custody a suspect, a man with the surname Zhao, born in 1990. They said the man was a graduate of the middle school and that he had told the police he was seeking revenge because he was bullied during his time there.

“He attacked students because he hated the students there,” said a statement by the Mizhi County police posted on Weibo, a popular microblogging site.