The head of Denmark's PET intelligence service said four men were arrested while a spokeswoman for Swedish intelligence agency Säpo said a fifth man had been arrested in Sweden in connection with the same plot against the Copenhagen-based Jyllands-Posten daily.
"It is our sense based on intelligence that this is a militant Islamic group with links to international terrorist networks," PET head Jakob Scharf told reporters.
They were planning an attack "within the next few days," PET said in a statement.
In an email to Danish news agency Ritzau, Danish justice minister Lars Barfoed said the arrest of the suspects, who had a "militant Islamist background," prevented what could have been the most serious attack to ever occur in Denmark.
Scharf told the news conference "the plan was to try to gain access to the location of Jyllands-Posten in Copenhagen and to try to carry out a Mumbai-style attack."
The 2008 attacks in Mumbai saw 10 heavily armed gunmen storm three luxury hotels, the city's main railway station, a popular tourist restaurant and a Jewish centre. The siege, centered around the luxury Taj Mahal Palace hotel, killed 166 people.
Several of the suspects could be described "as militant Islamists with connections to international terror networks," PET supremo Jakob Scharf said.
"These arrests have successfully stopped an imminent terror attack, where several of the suspects ... were going to force their way into the (building which houses Jyllands-Posten) in Copenhagen and kill as many people as possible," Scharf was quoted as saying in the PET statement.
The arrests took place in two suburbs of Copenhagen after a long investigation led in collaboration with Sweden's Säpo, PET said.
The man arrested in Stockholm is a 37-year-old Swede of Tunisian background.
Danish intelligence said the four men arrested in Denmark, in the Herlev and Greve suburbs of Copenhagen, were a 44-year-old Tunisian, a 29-year-old Swede born in Lebanon, a 30-year-old Swede and a 26-year-old Iraqi asylum seeker.
The first three were all living in Sweden and travelled to Denmark overnight. According to Jyllands-Posten's online edition, they travelled in a car rented in the Stockholm suburb of Kista.
Also at the Copenhagen press conference, Saepo head Anders Danielsson said the men based in Sweden had been under surveillance.
“We learned that people in Sweden were planning a terror crime in Denmark. We've known about it for several months. These people are known to the police in Sweden. We contacted our Danish colleagues. We've had people under intense surveillance. The whole time we've been working under the theory tha they would travel from Stockholm to Copenhagen, which they did yesterday,” said Säpo head Anders Danielsson said on Wednesday.
Jyllands-Posten published a dozen cartoons in 2005 of the Prophet Muhammad that triggered violent and sometimes deadly protests around the world.
It has since been the target of many foiled attacks.
In January, a Somali man broke into the home of the cartoonist who drew the Prophet with a turban shaped like a bomb and allegedly threatened to kill him with an axe and a knife.
In September, a suspect detained in Norway confessed that he was planning an attack against Jyllands-Posten. In another case the same month, a Chechnya-born man was arrested in Copenhagen for preparing to send a letter bomb to the paper.
"The arrests underscore the serious terror threat against Denmark and especially against institutions and people connected to the cartoon case," Scharf said Wednesday.
"It is shocking for our employees and their families to once again see their place of work threatened," said Lars Munch, the chief executive of Jyllands-Posten's parent company, adding the company had "great confidence in the police and intelligence service efforts to protect us."
PET said police seized plastic strips to be used as handcuffs, a machine gun and live ammunition in connection with the arrests.
“We knew there were weapons in the car and the whole time we were ready to break off following them and take action if something had changed,” said Säpo's Daneilsson.
“It was a balancing act between preventing an act of terrorism from being carried out and collecting evidence that could be used against the suspects which would hold up in court.”
An Islamist militant who killed only himself in Sweden's first suicide attack on December 11 said in a message sent before his death he wanted to punish Swedes for their "support of the pig Lars Vilks," referring to a Swedish cartoonist who drew an image of the Prophet Mohammed with the body of a dog in another publication.
Säpo spokeswoman Katarina Sevcik said the group arrested on Wednesday "have up to now no known connection to the events of December 11."
The PET said last month that there were "renewed indications that terrorist groups abroad are looking to send terrorists to Denmark to commit terrorist attacks."
"Denmark and Danish interests are a priority terrorist target," the agency said at the time.