Ms. Giasolli said the seniors stayed close to one another on Tuesday afternoon as SWAT teams cleared their school and then took them to a recreation center, where thousands of anxious parents waited to reunite with them. They hugged one another and broke down sobbing in groups as they learned that Mr. Castillo, also a senior, had died. Ms. Giasolli said the tight-knit class was now bonded by grief and shock.

“We’re all shaken,” she said. “It happened right before our eyes. It happened right in our classroom. We’re all hurting. We all did know these kids. We did know the shooter. We did know the people who got hurt. We’re all hurting in our own ways.”

The suspects carried at least two handguns, and at least one of them had been restrained by a school security officer by the time law enforcement arrived, Sheriff Tony Spurlock of Douglas County said.

Sheriff Spurlock said deputies had to force their way into the school because it was locked down. Law enforcement happened to choose a door that was near the shooting scene and quickly apprehended one of the suspects, who was identified as Devon Erickson, 18.

The contrast between the tributes to Mr. Castillo, who was hailed as a hero by his friends, his community and Colorado’s governor, and the first court appearance of the two suspects could hardly have been starker.

Mr. Erickson entered court on Wednesday wearing a red jumpsuit draped over his thin frame. His hair, dyed pink and blue, hung over his face. He sat between his lawyers with his head hanging, and he was shackled at both the wrists and the ankles. At times his body shook ever so slightly.