SALEM, Mass. — A Massachusetts teenager was convicted Tuesday of raping and killing his high school math teacher.

Philip Chism was 14 when he followed his algebra teacher, Colleen Ritzer, into a school bathroom, strangled her, stabbed her at least 16 times and raped her.

During his trial, his lawyer admitted Chism killed Ritzer, but said he was suffering from severe mental illness and was not criminally responsible for his actions. A psychiatrist who testified for the defense said Chism was hearing voices and in the throes of a psychotic episode when he killed Ritzer.

Ritzer, 24, was a popular teacher at Danvers High School, about 25 miles north of Boston, doing her dream job, teaching math, Assistant District Attorney Kate MacDougall told jurors in her opening statement.

On Oct. 22, 2013, Ritzer asked Chism to stay after school. Another student who also stayed late that day testified that Ritzer tried to engage Chism — a student who had recently moved to Massachusetts — in light talk about how his new community compared with his old town of Clarksville, Tennessee. The student said Chism became visibly upset and Ritzer eventually changed the subject.

A short time later, Ritzer left the classroom. Jurors were shown chilling surveillance video that showed Ritzer walking down the hallway to the bathroom. Seconds later, Chism is seen poking his head out of her classroom and looking down the hall. He then goes back into the classroom, but comes right back out with his hood on. Chism is then shown putting on a pair of gloves as he walks into the bathroom. Minutes later, he is shown emerging from the bathroom carrying the black pants Ritzer was wearing.

Chism, now 16, was tried as an adult.

A bloody box cutter, mask, gloves and a hooded sweatshirt were later found in his backpack by police who found Chism walking beside a highway in neighboring Topsfield.