ANNAPOLIS, Md. — Five people were killed Thursday afternoon in a shooting at the Capital Gazette newsroom in Annapolis, Md., and several others were “gravely injured,” said William Krampf, acting chief of the Anne Arundel County Police Department.

Investigators identified the suspect, an adult male, using facial recognition technology, according to a law enforcement official. The suspect’s name has not been released.

The suspect is in custody and is being questioned, said Steve Schuh, the county executive, and there was no information on motive as of late Thursday afternoon. Mr. Schuh said the gunman was found hiding under a table. He used a shotgun in the attack, according to another law enforcement official.

“We did recover what we thought may have been an explosive device,” Mr. Krampf said at a news conference. “That has been taken care of. We have members of the bomb squad on scene. We don’t anticipate having any more explosive devices.” More than 170 people were in the building at the time, he said.

The suspect did not exchange gunfire with officers when he was taken into custody, said Lt. Ryan Frashure, a police spokesman.

Phil Davis, a crime reporter at The Capital, was in the newsroom during the attack.

“The fact that I’m here, I’m too lucky,” Mr. Davis said in an interview on Thursday. He hid under his desk hoping the gunman would not see him, he said, as did many of his colleagues in the back of the newsroom.

The gunman began shooting his colleagues seated in the front. “He never said anything,” Mr. Davis said.

“He didn’t have enough bullets for us,” Mr. Davis said. “It was terrifying to know he didn’t have enough bullets to kill everyone in that office, and had to get more.”

For about two to 10 minutes, there was calm, no one was moving and no one was saying anything, Mr. Davis said. The police arrived, and the journalists put their hands up and shouted, “We’re not him.”

On his way out, Mr. Davis saw three of the five colleagues who were killed, he said.

“Thankfully, the ones who are left are close,” he continued. “But we’re still concerned about a lot of people.”

Shortly before 6 p.m., at least three helicopters were still circling, and lines of silent police cars, with lights flashing, blocked off the main roads leading up to 888 Bestgate Road, the sprawling brick office building where the shooting took place. Yellow police tape flapped in the wind, keeping the public and journalists away from the area.

Earlier, aerial video showed a heavy police presence, as well as lines of people with their arms up in the air walking away from the area.

One of the first tweets posted by someone in the building was from Anthony Messenger, a summer intern at the newspaper. At 2:43 p.m., he wrote: “Active shooter 888 Bestgate please help us.”

Jahnae Miller, 23, a teacher from Hyattsville, said she was at her doctor’s office on the first floor of the building when nurses and other workers started shouting to turn off the lights because there was a shooter down the hall. They turned off the lights, and she hid under a desk in an inner room for about an hour. They barricaded the doors with filing cabinets that the police had to push out of the way to get inside.

“I was afraid, I was crying,” she said. “I teach elementary school, and lockdown drills have become normal, unfortunately. To be in a situation — that it’s not a drill this time. To feel those emotions going through and not knowing where the shooter is, it was kind of overwhelming.”

The building is mixed use with various offices, including those of a pediatric orthodontist and a lawyer.

The episode prompted the police departments in some major American cities, including Los Angeles and Chicago, to send increased security to their area media organizations.

The New York Police Department deployed counterterrorism teams to news media organizations in and around New York City, a decision that has become standard practice during terrorist and active shooter events. The latest deployments were not based on a specific threat, the N.Y.P.D. said in a statement, but rather “out of an abundance of caution until we learn more about the suspect and motives behind the Maryland shooting.”

Mayor Gavin Buckley of Annapolis sent “love and thoughts to all the families who’ve been affected by this.”

He commended the emergency workers, who he said had recently run through active shooter training. “We did not expect something like this to happen in our community, but we were ready,” he said.

Gov. Larry Hogan of Maryland said he was “absolutely devastated to learn of this tragedy.”

President Trump said on Twitter that he had been briefed on the shooting. “My thoughts and prayers are with the victims and their families,” he said.

On Twitter Thursday evening, Sarah Huckabee Sanders, the White House press secretary, called the attack “evil” and said: “A violent attack on innocent journalists doing their job is an attack on every American.”

The Capital Gazette’s website directory lists 55 newsroom and business-side employees.

In a tweet late Thursday afternoon, Danielle Ohl, a Capital reporter, said: “The Capital is not a big newsroom. There are about 20 news staffers, a few more advertising. We are close. We are family. I am devastated.”

And Paul Gillespie, a staff photographer at Capital Gazette Communications, tweeted: “I am okay physically, so far, mentally I am a mess. I was inside. I am lucky to be alive.”

Capital Gazette Communications publishes a daily newspaper, The Capital, the twice-weekly Maryland Gazette, as well as a website and other papers serving other local areas. It is owned by the Baltimore Sun Media Group. The company, one of the oldest publishers in the country, was founded in 1727, according to its website.

Carlos Linares, 70, a mechanical engineer, was in a conference room at a law firm on the fourth floor when the shooting began.

He heard “some loud noise, loud footsteps, a commotion that you usually don’t hear in a lawyer’s office,” he said. “I thought, that’s strange.”

He stepped out and saw that everyone was huddled in the front lobby, he said. Several of the men and women were upset and agitated, he said. He was told that there was an active shooter on the first floor.

He and the others were escorted out of the building about 30 minutes later, and led across the street to the Annapolis Mall to the Lord & Taylor store, where people in the area were being reunited with their families.

It was only when he saw all the police officers, SWAT team and bomb squad that Mr. Linares realized the extent of the shooting. “I didn’t know how big it was,” he said. “I had never seen so many police in my life.”

Randall Fisher, a lawyer and owner of Fisher Law Office in Suite 420, said he and another worker barricaded themselves inside his office suite, pushing his desk up against the door. He watched from the window as police in riot gear led groups of people away from the building with their hands up.

“All this stuff, this was just crazy,” Mr. Fisher said.

This is a developing story and will be updated.

Sabrina Tavernise reported from Annapolis, Md., Maya Salam from New York, and Erica Green from Washington. JoAnna Daemmrich contributed reporting from Annapolis, Adam Goldman from Washington, and Julia Jacobs from New York.