On that morning in 2017, students in the back of the classroom were throwing crumpled paper and broken pencils , which Mr. Cedeno said he thought were aimed at him. Mr. Cedeno said he confronted the students, which led Matthew to move toward him. Fearing for his life, Mr. Cedeno said, he grabbed his knife, flicked open the blade and “just waited.”

During closing arguments last week, Nancy Borko, the lead prosecutor, said Mr. Cedeno was looking for a fight with “his trusty new knife.”

Matthew’s death, the first homicide in a New York City school in two decades, prompted protests from parents over the lack of metal detectors at a school with a history of violent incidents. Members of the L.G.B.T.Q. community argued the school should have taken action against those who had bullied a gay student.

Christopher Lynn, one of the defense lawyers, said in closing arguments that Mr. Cedeno “did not attack anyone that day.” Instead, he had tried to de-escalate the onslaught, as he often did, by leaving class in the midst of a pummeling. But when he returned, Mr. Cedeno was hit with debris again, Mr. Lynn said. “He was attacked and never the aggressor,” Mr. Lynn said.

Mr. Lynn and Robert J. Feldman built a defense on the premise that Mr. Cedeno had suffered unchecked taunts in the classroom that drove him to take extreme action.

The two lawyers took the case, pro bono, at the behest of Councilman Rubén Díaz Sr., who has been criticized for making homophobic remarks but who had helped Mr. Cedeno post bail soon after he was charged. Mr. Cedeno lives in Mr. Díaz’s district in the Bronx .

Mr. Feldman framed the proceedings as “a gay-pride trial.” He and Mr. Cedeno often wore matching rainbow heart pins clipped to their lapels. Tom Shanahan, who is representing Mr. Cedeno in a lawsuit against the Department of Education and joined the defense table, also wore rainbow-colored accessories .