A man firing a shotgun and armed smoke grenades killed four journalists and a staffer at Maryland's capital newspaper The Capital Gazette, then was swiftly taken into custody by police who rushed into the building.

Police said the suspect in custody is a white man in his late 30s.

Acting Police Chief William Krampf of Anne Arundel County called it a targeted attack in which the gunman “looked for his victims".

“This person was prepared today to come in, this person was prepared to shoot people,” Krampf said.

Journalists crawled under desks and sought other hiding places in what they described as minutes of terror as they heard the gunman's footsteps and the repeated blasts of the shotgun as he moved about the newsroom.

Those killed included Rob Hiaasen, 59, the paper's assistant managing editor and brother of novelist Carl Hiaasen.

Carl Hiaasen said he was “devastated and heartsick” at losing his brother, “one of the most gentle and funny people I've ever known".

Also slain were Gerald Fischman, editorial page editor; features reporter Wendi Winters; reporter John McNamara, and sales assistant Rebecca Smith.

The newspaper said two other employees had non-life threatening injuries and were later released from a hospital.

Krampf said the gunman was a Maryland resident but didn't name him.

Separately, a law enforcement official said the suspect was identified as Jarrod W. Ramos. The official wasn't authorised to discuss the ongoing investigation and spoke on condition of anonymity.

Phil Davis, a court and crime reporter for the paper, tweeted that the gunman shot out the glass door to the office and fired into the newsroom, sending people scrambling under desks.

“There is nothing more terrifying than hearing multiple people get shot while you're under your desk and then hear the gunman reload,” he wrote in a tweet.

In a later interview appearing on the paper's online site, Davis likened the newspaper office to a “war zone.”

“I'm a police reporter. I write about this stuff not necessarily to this extent, but shootings and death all the time,” he said. “But as much as I'm going to try to articulate how traumatising it is to be hiding under your desk, you don't know until you're there and you feel helpless.”

Reporter Selene San Felice told CNN she was at her desk but ran after hearing shots, only to find a back door locked. She then watched as a colleague was shot, adding she didn't glimpse the gunman.

“I heard footsteps a couple of times,” she said. “I was breathing really loud and was trying not to, but I couldn't be quiet.”

The reporter recalled a June 2016 mass shooting attack on Orlando's gay nightclub Pulse and how terrified people crouching inside had texted loved ones as dozens were killed.

Said San Felice, “And there I was sitting under a desk, texting my parents and telling them I loved them.”

Survivors said the shooting though it seemed agonisingly long lasted mere minutes. And police said their response was swift.

Police spokesman Lt. Ryan Frashure said officers arrived within about 60 seconds and took the gunman into custody without an exchange of gunfire. About 170 people were then evacuated from the building, which houses other offices, many leaving with their hands up as police and other emergency vehicles arrived.

At the White House, spokeswoman Lindsay Walters said: “There is no room for violence, and we stick by that. Violence is never tolerated in any form, no matter whom it is against.”

Hours later, investigators remained on the cordoned-off site early on Friday as they sought clues to the gunman's motives.

“The shooter has not been very forthcoming, so we don't have any information yet on motive,” Anne Arundel County Executive Steve Schuh said.

In 2012, Ramos filed a defamation lawsuit against the newspaper, alleging he was harmed by an article about his conviction in a criminal harassment case a year earlier. The suit was dismissed by a judge who wrote Ramos hadn't shown “anything that was published about you is, in fact, false.”

An appeals court later upheld the dismissal.

Annapolis Mayor Gavin Buckley said the community was grieving the attack on its paper.

“These are the guys that come to city council meetings, have to listen to boring politicians and sit there,” Buckley said. “They don't make a lot of money. It's just immoral that their lives should be in danger.”

The newspaper is part of Capital Gazette Communications, which also publishes the Maryland Gazette and CapitalGazette.com. It is owned by The Baltimore Sun.

The Associated Press Media Editors promised to help Capital Gazettejournalists as they recover. An APME statement called on newspapers nationwide to help the paper continue its community coverage and fight for freedom of the press.

Thursday's attack on The Capital Gazette in Annapolis came amid months of verbal and online attacks on the “fake news media” from politicians and others from President Donald Trump on down.

It prompted New York City police to immediately tighten security at news organisations in the nation's media capital.