A gunman killed a sixth-grade student and injured five other people at a high school in Perry, Iowa, early Thursday morning just as students were arriving back to school after their winter break.

Four of the injured were students, and one was an administrator, Mitch Mortvedt, assistant director of the Iowa Division of Criminal Investigation, said at a news conference on Thursday. One of the injured victims was in critical condition.

The administrator was identified by Easton Valley Community School District as Dan Marburger, the principal at Perry High School, where the shooting took place. Officials did not release the names of any other victims.

The gunman, identified as Dylan Butler, a 17-year-old student at the high school, died from a self-inflicted gunshot wound, Mr. Mortvedt said. Law enforcement officials believe he acted alone and said the motive for the attack was not yet known.

In Perry, a town of 8,000 people about 40 miles northwest of Des Moines, the shooting sent parents rushing to schools, businesses closing their doors early and, by evening, residents gathering for candlelight vigils.

Hundreds of people huddled in the cold at a park late Thursday, where ministers led the group in prayer and residents shared their accounts of what had taken place that day. One high school student told the crowd that she heard the gunshots that morning and thought that they were the sounds of balloons popping. Another speaker said she knew the student who had been killed, “an amazing little boy, and he was the sweetest.”

Laura Espinoza, 38, an educator at Perry Elementary School, said that she knew Mr. Marburger, the principal who was shot. He was affable, friendly and eager to get to know his students, she said.

“I feel like a lot of times you say the principal is this very serious authority figure, which he is, but he’s also a person you can hear a joke from,” she said. “Or you could tell a joke and he’ll laugh about it.”

Ms. Espinoza said that when the school went into lockdown that day, she went into “panic mode,” trying to protect her students while frantic about her own children’s safety.

“You just don’t imagine it’ll happen to you,” she said. “I feel like we go into these trainings ready to learn, but hoping that you’ll never have to use that.”

The shooting unfolded just as the school day was beginning. The authorities said calls about someone with a gun at Perry High School began coming in just after 7:30 a.m. and emergency responders arrived about seven minutes later at the school.

The attack took place before classes began, Mr. Mortvedt said during a breakfast program that catered to students from multiple grades where the high school and middle school share a campus.

He said that when law enforcement officials arrived on the scene, they found multiple victims with gunshot wounds, and students and faculty sheltering in place and running from the school.

They also found a “rudimentary” improvised explosive, Mr. Mortvedt said, and agents with the state fire marshal and Bureau of Alcohol, Tobacco, Firearms and Explosives “rendered the device safe.”

The gunman was armed with a pump-action shotgun and a small caliber handgun, Mr. Mortvedt said. He also made “a number of social media posts in and around the time of the shooting,” which law enforcement officials are investigating, Mr. Mortvedt said.

About 150 law enforcement officers responded to the scene, Mr. Mortvedt said.

Governor Kim Reynolds said at a news conference on Thursday that the shooting “has shaken us to our core” and recognized the “incredible coordination” of local, state and federal agencies.

The victims’s families “need your thoughts and prayers, as well as time and space to process and grieve,” said Chief Eric Vaughn of the Perry Police Department, holding back tears. “This community has been through tough times before and have rallied together. I’m sure this time will be no different.”

As of Thursday afternoon, multiple patients with gunshot wounds were being treated at Iowa Methodist Medical Center and MercyOne Des Moines Medical Center, Polk County Medical Coordination Center said in a statement.

Ava Augustus, a senior at Perry High School, told WHO 13 News that she was in her counselor’s office when she heard three gunshots. When they got the all-clear, she saw glass everywhere, blood on the floor, and a student who had been shot in the leg being taken out of the auditorium.

Jody Kurth told KCCI 8 News, a local CBS affiliate, that her stepson, a student at Perry High School, was hurt in the shooting. She described the morning attack as “an absolute nightmare.”

Her daughter texted her to let her know about the shooter, she said, calling it “one of the worst moments of my entire life.”

Both of her children were safe, she told KCCI.

At a community center in Perry, a bus dropped off students from the elementary school, where parents waited for their children to arrive. One of the first parents was Amanda Woods, 34, a mother of two young sons.

“At first I wasn’t sure which school it was at,” said Ms. Woods, who had been listening to the police scanner to get more information of the shooting. “I was freaking out.”

The shooting added an extra layer of national attention to the small community of Perry, where about 8,000 people live. Media organizations were already in the area on Thursday to cover a scheduled campaign event by the Republican presidential candidate Vivek Ramaswamy, who hosted a prayer circle just hours after the attack. Attendees expressed fear, but little surprise.

“God please help our country,” said Mr. Ramaswamy as he addressed the prayer event and shook hands with everyone, calling it a “somber day.”

Mr. Ramaswamy was in Iowa ahead of the state’s Republican caucuses, scheduled for Jan. 15.

Karine Jean-Pierre, the White House press secretary, said President Biden had been briefed on the shooting.

“Our students and teachers deserve to know that their schools are safe spaces and to focus on learning — not duck and cover drills,” she said at a briefing on Thursday.

In a statement on social media, Senator Chuck Grassley of Iowa called the “appalling violence” at Perry High School “heartbreaking” and thanked school and law enforcement officials for their quick response.

Shelbie Lehman came into Mr. Ramaswamy’s event with her partner after picking up her first grade daughter from elementary school after hearing about the shooting at the high school.

“It’s very scary having them walk your kid out with a gun and officer, and having seven, eight cops there,” Ms. Lehman told Mr. Ramaswamy.

Ms. Lehman said it was hard to explain to her daughter why there were so many police and why she was picked up from school. She said she planned to try to explain things to her daughter later today as best she could.

The Perry Community School District said the high school, middle school and elementary school would be closed on Friday.

Remy Tumin and Victor Mather reported from New York, and Leah McBride Mensching from Perry, Iowa. Molly Longman contributed reporting from Perry, and Julie Bosman from Chicago.