HIGHLANDS RANCH, Colo. — He was a high school senior, set to graduate this week.

But instead of celebrating Kendrick Castillo's graduation, the family of the 18-year-old will be mourning his death.

Castillo, a senior at STEM School Highlands Ranch in suburban Denver, was killed in a classroom Tuesday in a shooting that police say was carried out by a pair of his classmates: 18-year-old Devon Erickson and a younger student who has not been identified because she is a juvenile. Eight other students were injured.

The shooting happened at the end of the school year in a British literature class. The toll exacted by the shooters could easily have been worse. Deputies arrived within two minutes of the first call, officials said. A security guard restrained a shooter. And accounts provided by students and by a military official suggest that Castillo and several classmates sprang into action when the shooters began firing.

It marked the second time in a week that targets of a shooting at a school appeared to play a role in quelling it: One of the students killed in a shooting last week at the University of North Carolina at Charlotte has been hailed for his role in thwarting the attacker there.

Nui Giasolli was in class in Colorado when she said a classmate began shooting. She told NBC’s Today that Castillo lunged at the man who opened fire and was shot. Several other classmates also worked to take down the shooter, she said.

"The next thing I know, he's pulling a gun and telling nobody to move," Giasolli said. "That's when Kendrick lunged at him, and he shot Kendrick, giving us all enough time to get underneath our desks to get ourselves safe, to run across the room to escape."

Another student, Brendan Bialy, who is 18 and a member of the Marine Corps Delayed Entry Program, helped subdue one of the shooters, the U.S. Marine Corps confirmed. Leadership and "a bias to action" were things Bialy, who is expected to ship out to boot camp this summer, had been talking about with his recruiter since he joined the program in July and began training and preparing for the service, said Capt. Michael Maggitti.

Bialy was in class when the attack happened, Maggitti said, "and he decided to step up and take action" to protect his classmates and teachers. Maggitti said his understanding is that Bialy and several other students "helped put an end to the tragedy."

In a tweet Wednesday, President Donald Trump offered his condolences. In the past, he has advocated for "hardening" schools by adding security measures and for arming teachers to deter shootings.

Ivanka Trump, the president’s daughter and adviser, met with law enforcement officials Wednesday in Colorado “to offer the country’s gratitude for their heroic actions,” a White House statement said.

The charter school campus has more than 1,800 students in kindergarten through 12th grade, including small children who streamed out of the school Tuesday, weeping and with their arms raised to escape the danger.

The shooting happened not far from Columbine High School, in a community still on edge after marking the 20th anniversary of a shooting that stunned the country.

District Attorney George Brauchler, who grew up in the region, said at a news conference Wednesday that if someone had told him that in 20 years the region would endure several mass shootings, he would have thought them crazy. "These are aberrant acts," he said.

Now is the time to mourn and to weep, he said, but the people of Colorado are resilient. "My kids are going to go to school today," he said, acknowledging the conversations anxious parents are having. "I recommend everyone else send their kids, too."

Details of the shooting began to emerge Wednesday.

Within two minutes, the first deputy arrived and confronted a gunman, Douglas County Sheriff Tony Spurlock said. Deputies struggled with the suspects before taking them into custody, he said.

Neither suspect was injured, he said, and no gunfire was exchanged. One suspect was restrained by the armed private security officer hired to protect the school, Spurlock said.

"I have to believe the quick response of the officers that got inside that school helped save lives," Spurlock said.

About 600 students were in the section of the school where the attack unfolded. Authorities are still interviewing witnesses and learning what happened, the sheriff said. “We are going to hear about very heroic things that happened at that school.”

The wounded students were 15 years old or older, he said.

Most patients admitted to hospitals have been discharged. Two remained hospitalized Wednesday, one in fair condition and one in good condition, said Wendy Forbes of Littleton Adventist Hospital.

Sasha Weiden, a sixth grader at the school, said his physical education class was in the middle of a game Tuesday when students saw a stampede of high schoolers sprint past. Then, they heard the lockdown announcement: "Locks, lights, and out of sight."

He and about 10 other children hid in a closet. He tried to huddle near the wall. "It was really dark, and it was extremely quiet," he said. "I heard some emergency door alarms go off, then I heard some loud booms."

His teacher, Sasha said, "had a tennis racket in case the shooter did come."

After about 15 minutes, when he heard someone unlock the door, he thought it was the shooter.

But it was police, holding guns, who ushered them out past the smashed glass in the front door to a big green field, where they waited. Some people were crying, he said, and he tried to comfort them. "I was really scared."

Students' responses to the shootings in Colorado and North Carolina opened a window into the ongoing debate over how to make schools safer and how to contend with a shooter.

Greg Crane, founder of ALICE Training Institute, which stands for Alert, Lockdown, Inform, Counter, and Evacuate, said he believes people should be trained to be proactive in shootings.

“It’s the option of doing nothing vs. doing something,” Crane said. “Over the years, we have unfortunately learned — at Columbine, Sandy Hook, Virginia Tech — that if we teach staff and students to do nothing and wait for police, the death rates would be much, much worse.”

Kenneth Trump, president of National School Safety and Security Services, a consulting firm that conducts school security and emergency planning assessments, said he believes lockdowns are the best way to respond to a school shooting. But he said training that includes running, hiding and fighting a gunman — it's called options-based training — is gaining popularity.

He said he believes asking students to make a life-or-death decision to fight a gunman can be problematic, given that students' brains have yet to fully develop.

He said most schools do not teach children to fight a gunman.

"I consider options-based training a high-risk and high-liability proposition," he said.

Michael Dorn, a senior analyst at the school security consulting firm Safe Havens International, works with school districts and states to develop safety plans. He said the training touches on myriad ways to respond to a shooter, including tackling the gunman.

But he cautioned against that. He said that most gun incidents in schools are not shootings and that there have been instances in which a person with a gun is engaged, resulting in shots being fired.

"The concept can work, but we caution people because we have had quite a few people shot and killed by attempting this," he said. "We are taking great pains in teaching people when not to take that approach."

The Washington Post’s Jessica Contrera, Jennifer Jenkins, and Perry Stein contributed to this article.