Maybe this Fourth of July would be different. The gun violence that has plagued Philadelphia has been inching down, steadily. Perhaps Independence Day would pass without the bloodshed that has marred it in recent years.

But sometime after 8 p.m. on the eve of the Fourth, in a neighborhood in southwest Philadelphia, someone dressed in a ski mask and body armor opened fire on a street, and bringing any sense of festivity in the neighborhood to a horrifying end.

The suspected shooter, using an assault-style rifle, killed five people, ages 15 to 59, and wounded two others, firing seemingly at random, hitting a car carrying a family on their way home and those simply walking by, the police said. Officers arrived to find what they described as “an extensive scene” of carnage, and chased the suspect into an alley, where the person was arrested.

The suspect, Kimbrady Carriker, is being charged with more than 30 counts, including murder, attempted murder and assault, the district attorney’s office said.

In initial reports, police described the suspect as a 40-year-old male, but authorities later clarified that they were unsure of the suspect’s gender identity and in a news conference on Tuesday used the pronouns “they/them.”

While so much of Philadelphia’s gun violence is driven by arguments and retaliation, the shooter in this case appeared to be firing aimlessly, according to Larry Krasner, the Philadelphia district attorney.

“At first look, it seems to bear the characteristics of a lot of random mass shootings that occur in the United States,” he said in an interview on 56th Street in the Kingsessing neighborhood where the shooting took place, adding, “This does not appear to be a whole bunch of people who knew each other very well.”

The five who were killed were Lashyd Merritt, 20; Dymir Stanton, 29; Ralph Moralis, 59, Dajuan Brown, 15, and Joseph Wamah, Jr., 31. Two children, aged 2 and 13, were hospitalized with injuries and were in stable condition.

In a neighborhood where hearing gunshots is not uncommon, witnesses said the volume and length of the barrage was striking. About 50 spent shell casings were found at the crime scene, which covered a two-block by four-block area, authorities said.

On Tuesday morning, Theo James, 25, said that he had seen the shooter and heard gunfire unlike any he had heard before: “The shots sounded like this was a military base here on the corner.”

The shooter, he said, appeared to be firing at people at random. “He was chasing people around,” Mr. James said.

One of the those killed, Mr. Merritt, had just graduated from high school and had been working from home for the Internal Revenue Service, according to his brother-in-law, Dominique Evans. Mr. Merritt was on his way to the store from his home when he was shot and killed.

“He was a kind person,” Mr. Evans said. “Very caring, smart.”

Willa Mae Dill, who lives in the neighborhood, said her nephew, Mr. Stanton, was “good with people.” A sports fan, he had a girlfriend and a 4-year-old daughter, and visited Ms. Dill at her home most days.

Omar Davis, 60, was chatting with his friend Mr. Moralis, whom he knew as “Rab,” moments before gunfire broke out on Chester Avenue.

Mr. Davis said he had known Mr. Moralis for at least 47 years. He worked in restaurants. “Good dude,” he said. “He grew up in this community, everybody knows him.”

The shooting was one of at least 348 incidents across the nation this year in which four or more people were injured or killed, according to the Gun Violence Archive.

In Philadelphia, the number of gun homicides had soared in recent years and the crisis dominated the recent mayoral election.

The city has sought to address the violence with grants for community groups, violence intervention programs and earlier curfews. It has sued the state legislature for pre-empting its authority to enact stronger local gun laws, like reporting requirements for lost or stolen guns.

The number of guns in the city has made the problem difficult to tackle, and at the news conference on Tuesday afternoon, Jim Kenney, the outgoing mayor, begged Congress to pass measures to reduce the ready accessibility of guns.

“This country needs to re-examine its conscience and find out how to get guns out of dangerous people’s hands,” he said.

There are signs that Philadelphia’s gun violence epidemic may be waning. There have been 212 homicides in 2023 so far, a decline of 19 percent from 2022, according to the Office of the Controller.

But for the families of those who were killed, those numbers offer little solace.

Dajuan Brown, 15, a rising sophomore at the Jules E. Mastbaum Area Vocational/Technical School, was staying at his grandmother’s house in the southwest section of the city for the summer.

The teenager “had his own little spice on his dancing,” said his mother, Nyshyia Thomas, 34. “You were sad around him, he wasn’t letting you be sad.”

“My baby was only 15 years old,” his mother said. “And I just won’t get to see my son anymore.”

Amy Harmon and Livia Albeck-Ripka contributed reporting. Kirsten Noyes and Sheelagh McNeill contributed research.