CHARLOTTE — Facing a gunman in his classroom, Riley Howell chose to fight.

Authorities said Wednesday that the 21-year-old student at the University of North Carolina’s campus here knocked the assailant off his feet at a pivotal moment in Tuesday’s shooting, saving the lives of others even as Howell himself received a mortal wound.

Kerr Putney, chief of the Charlotte-Mecklenburg Police Department, described Howell as “the first and foremost hero” in an incident that left two dead and four injured but could have turned out far worse.

“But for his work, the assailant may not have been disarmed,” Putney said of Howell in a televised news conference. “Unfortunately, he gave his life in the process. But his sacrifice saved lives.”

Putney said Howell, of Waynesville, N.C., was probably the second UNC-Charlotte student to die in the shooting that occurred in the Kennedy Building shortly after a class began at 5:30 p.m. The other fatality, officials said, was Ellis “Reed” Parlier, 19, of Midland, N.C.

The shooting rocked the 29,000-student public university on the final day of spring semester classes. Exams were postponed through Sunday, but the university said commencement will proceed as planned on May 10 and 11.

“This is still very raw for us,” UNC-Charlotte Chancellor Philip L. Dubois said at the news conference. Dubois said he had visited with three injured students who remained in the hospital. “They’re all doing okay,” he said. A fourth had been hospitalized and released.

The gunman targeted an anthropology class for reasons that remain unknown.

“Yes, there was a shooting in my class,” Adam P. Johnson, an anthropology instructor, wrote in a tweet. Johnson was teaching LBST2213, a course on science, technology and society, in Kennedy’s Room 236. “My students are so special to me and I am devastated,” Johnson wrote.

Police identified the suspect as Trystan Andrew Terrell, 22. Local media described Terrell as a former UNC-Charlotte student, but efforts to confirm that with the university were unsuccessful. Charlotte-Mecklenburg police said he was charged with two counts of murder, four counts of attempted murder, four counts of assault with a deadly weapon with intent to kill, possession of a firearm on educational property and discharging a firearm on educational property. Terrell is due in court Thursday.

Relatives of Terrell could not be reached for comment.

Putney said the gunman’s motive was unclear. “We can’t really discern the why just yet,” the chief said. He said the gunman had used a legally purchased handgun. The chief said he was concerned about the shooting’s apparent “randomness.”

“Everyone in this community stands in shock and grief,” Charlotte Mayor Vi Lyles said. She said the city has joined others in recent years stricken by mass gun violence, including Las Vegas and cities in Florida. “Now, it’s Charlotte,” she said. “We have to support each other in the days and weeks to come.”

Dubois called it the “saddest day in UNC-Charlotte’s history.”

UNC-Charlotte’s police force, like its counterparts across the country, has trained intensively to respond to campus shootings in recent years. Jeffrey A. Baker, chief of the university’s Police & Public Safety agency, said his officers responded swiftly and were able to stop the gunman. Authorities praised the actions of campus police Sgt. Richard Gundacker, who was first on the scene, and others who secured the building and helped subdue the gunman.

Baker said officers followed their training. “We don’t sit,” Baker said. “We don’t wait. We go. And we’re going to go to the assailant. And that’s exactly what they did.”

Gundacker told reporters: “I’ve been preparing for this over 20 years. . . . The training pays off, and that’s why we do it.”

Baker said officers were able to get quickly to the building where the shooting happened because they already were converging for a Waka Flocka Flame concert on campus. The rapper’s performance was canceled.

After shots were fired at 5:40 p.m., the campus quickly locked down. Officials sent warnings via text messages, email and other channels. The message — “Run, Hide, Fight” — urged people to run to safety, hide from potential assailants or, as a last resort, fight.

Howell, authorities said, chose confrontation. “Having no place to run and hide, he did the last,” Putney said.

Howell’s major was environmental studies. He enrolled at UNC-Charlotte in fall 2018 as a community college transfer. Howell’s family told WSOC-TV in a statement that the 21-year-old “loved all things outdoors, adventure, and especially family. He loved to work outside, and when he worked, he did it with his hands and his heart. He always was able to put others before himself and never hesitated to help anyone who needed it.”

Parlier, who planned to major in computer science, had graduated in 2017 from Union County Public Schools in North Carolina. The university said he loved video games and aspired to become a game developer. Professors described him, the university said, as independent and motivated. “Our community is still in shock in dealing with this loss,” Tahira Stalberte, a spokeswoman for Union County schools, told the Greenville News.

Faculty have approved “degrees in memoriam” for Howell and Parlier.

On Wednesday afternoon, the UNC-Charlotte campus settled into a mournful quiet as students planned an evening vigil to honor the victims. There was a noticeable police presence, with officers on motorcycles stationed outside the Barnhardt Student Activity Center.

Uniformed officers also took posts at the front and back doors of the Kennedy Building. Nearby, Mike Le, 21, a senior from Trinity, N.C., stopped at a campus library to retrieve a bag he had abandoned after the shooting occurred.

Le recalled that he was outside the library late Tuesday afternoon when he suddenly saw people running.

“Then, I see someone bust open a door and I think, ‘Something’s got to be happening,’" Le said. “Someone said, ‘School shooter! School shooter!’ So I turned around and left, leaving my bag and everything.”

In the tumult, Le said he hopped into the car of someone he didn’t know who was parked nearby, joining three others who were in tears. He told the driver: “Take me anywhere. Somewhere else that’s not here.”

Just before the candlelight vigil at Halton Arena, floral bouquets were seen outside the Kennedy Building and at the base of a statue portraying the school’s gold-miner mascot, known as the Niner. The university’s homepage on the Web declared in a large headline: “Niner Nation Unites.”

Beth McGuire, 19, a first-year student from Concord, N.C., said it had been a painful and awkward day.

“Complete strangers have been coming up to me to ask if I’m okay,” she said. “It’s a good sense of community, I guess. It’s all about trying to see how everyone is doing. I’m okay, but I want to make sure everyone else is.”

Svrluga and Anderson reported from Washington. Debbie Truong contributed to this report.

This is a developing report. It will be updated.