The attack has been deemed as the deadliest day for journalism in America in several years.
Ramos had a long history of conflict with the daily, which produces a number of local newspapers along Maryland's shore.
He lost a defamation case against the paper in 2015 over a 2011 column he contended defamed him. The column provided an account of Ramos's guilty plea to criminal harassment of a woman over social media.
"This person was prepared today to come in, this person was prepared to shoot people," The Washington Post quoted Anne Arundel County Deputy Police Chief William Krampf said.
"His intent was to cause harm."
The police said all of the victims killed were Capital Gazette employees: Gerald Fischman, Rob Hiaasen, John McNamara, Rebecca Smith and Wendi Winters.
Fischman and Hiaasen were editors, McNamara was a reporter, Smith was a sales assistant and Winters worked for special publications, according to the newspaper's website.
Four of the victims died on the spot while the fifth was pronounced dead at the University of Maryland Medical Centre.
The shooting began at about 3 p.m. in the office building just outside downtown Annapolis, The Washington Post reported.
Ramos entered the building with a shotgun and looked for his victims, the police said.
The police, who arrived at the scene within a minute of the reported gunfire, apprehended Ramos found hiding under a desk in the newsroom.
Gazette reporter Phil Davis described the scene as a "war zone" and a situation that would be "hard to describe for a while".
After his arrest, Ramos refused to cooperate with the authorities or provide his name. He was identified using facial recognition technology, a law enforcement official told The New York Times.
President Donald Trump tweeted: "My thoughts and prayers are with the victims and their families," CNN reported.
House Speaker Paul Ryan said: "The senseless attack on a Maryland newspaper today is sickening. God bless these journalists. We pray for them and their families tonight."
Joel Simon, Executive Director of the Committee to Protect Journalists, said that violence against journalists was unacceptable.
"Newspapers like the Gazette do vital work, and our thoughts are with them amid this unconscionable tragedy," he added.
The Capital Gazette, which has an editorial staff of 31 people, had a daily circulation of about 29,000 and a Sunday circulation of 34,000 as of 2014.
Commonly referred to as the Capital, the paper was founded in 1884 as the Evening Gazette.
The paper promotes itself as one of the oldest publishers in the country, with roots dating to the Maryland Gazette in 1727.