Nikki Conroy, aged 12, and two classmates were stabbed repeatedly after Stephen Wilkinson launched his attack at Hall Garth school, Middlesbrough, in March last year.
Wilkinson ordered teacher Graham Nellist out at gunpoint, barricaded the door and ordered the children to kneel with their eyes closed. He then plunged a knife into Nikki and attacked Michelle Reeve, 13, and Emma Winter, 12.
Nikki, of Ridley Avenue, Middlesbrough, died almost instantly but Michelle and Emma were not seriously wounded.
David Robson, for the prosecution, said Wilkinson claimed "the person that was actually doing it was not S J Wilkinson but the dark side of his own nature, who he has always described as Wilson Jinks". Wilson Jinks is an anagram of S J Wilkinson.
Wilkinson's violent outburst came to an end when the deputy head, Chris Bielby, and a teacher, Dave Eland, burst in and overpowered him, holding him down until police arrived.
Wilkinson, 30, of Caversham Road, Middlesbrough, denies murder but admits manslaughter on the grounds of diminished responsibility. He also admits the attempted murder of Michelle and Emma.
Mr Robson said Wilkinson told police he could recollect nothing about the attack. But he later had flashbacks and told psychiatrists he had been "watching himself doing it".
The jury was told in a statement from Emma that just before the stabbing she heard Wilkinson say: "You are going to pay for what you have done to me." She closed her eyes and felt what she thought were punches. In fact, she was being stabbed.
The rest of the school was alerted when Mr Nellist, after being forced out of the class, shouted: "There's a nutter upstairs with a gun and he's got the kids." The teachers thought they would be putting the children's lives at risk if they charged into the classroom, but they were forced to act when Wilkinson launched his attack. Mr Eland and Mr Bielby burst through the door and grappled with Wilkinson before disarming him and pinning him down. The gun was later found to be a replica.
Mr Eland said that as he was being held, Wilkinson muttered: "Give me the knife and let me finish it." Mr Eland added: "I presumed this was his way of saying he wanted to commit suicide." The court was told that, as well as two knives and the replica gun used in the attack, another knife and a small axe were found in a holdall Wilkinson had with him.
The jury was warned to consider the "deeply sad tale" in a "cool, calm and detached way".
Referring to the stabbing of London headmaster Philip Lawrence, Mr Robson added: "In the light of the dreadful events over the weekend, there is a danger of emotion taking over. That you must not do."
An insight into the thoughts in Wilkinson's mind before the attack came from a six-page document found in his holdall. In what he described as "Thoughts/Confessions" dated January 1994, two months before he stormed into the classroom, he referred chillingly to the effect his Wilson Jinks alter-ego was having on him.
He wrote "Wilson Jinks wants to destroy me. If I let him gain ascendance, he will also slaughter the lambs."
Earlier he stated: "This confession may read like that of a lunatic but I can assure you that I am quite sane. In truth I wish only to convey to the world my absolute hatred of it. In doing so I shall steal the lives of society's weakest members; those most vulnerable and those most treasured."
The trial continues today.