Denmark's PET intelligence service said five suspects had been arrested and a submachine gun seized, preventing an imminent assault on the Copenhagen offices of the Jyllands-Posten daily.
The gunmen had wanted to kill as many staff as possible, said a statement from the agency.
Four men were arrested in Denmark while a spokeswoman for Swedish intelligence agency Saepo said a fifth was arrested in Sweden in connection with the same international plot.
"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", the agency said.
In an email to Danish news agency Ritzau, Danish Justice Minister Lars Barfoed said the arrests prevented what could have been the most serious attack ever to 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 ensuing massacre, centered around the luxury Taj Mahal Palace hotel, left 166 people dead.
"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 said in the PET statement.
The agency said police had also seized "plastic strips that could have been used as handcuffs, a sub-machine gun with silencer as well as ammunition".
Scharf added: "The arrests underscore the serious terror threat against Denmark and especially against institutions and people connected to the cartoon case."
Wednesday's arrests took place after an extended joint investigation with Saepo, PET said. The man arrested in Stockholm was a 37-year-old Swede of Tunisian background.
Danish intelligence said the four men arrested in Denmark 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.
They were arrested in the Herlev and Greve suburbs of Copenhagen.
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. Saepo knew there were weapons in the car used for the trip to Copenhagen, he added.
Later Wednesday, two Danish newspapers reported that Copenhagen police had evacuated a building in the Greve suburb, where the 26-year-old had been arrested, sealing it off and sending in a bomb-disposal robot.
Officers had found a suspicious object or explosives in the suspect's apartment, Politiken and Jyllands Posten reported on their websites. Local police refused to confirm the reports to AFP.
In 2005, Jyllands-Posten published a dozen cartoons of the Prophet Mohammed that triggered violent and sometimes deadly protests around the world.
Lars Munch, the chief executive of Jyllands-Posten's parent company, praised the work of the police and intelligence service.
"It is shocking for our employees and their families to once again see their place of work threatened," he said in a statement.
Saepo spokeswoman Katarina Sevcik said they had for the moment found no connection between the suspects arrested Wednesday and the December 11 bombing in which an Islamist militant blew himself up in Sweden's first suicide attack.
In a message sent before his death, the bomber said he wanted to punish Swedes for their "support of the pig Lars Vilks", a Swedish cartoonist who drew an image of Mohammed with the body of a dog in another publication.
The PET warned last month that there were "renewed indications that terrorist groups abroad are looking to send terrorists to Denmark to commit terrorist attacks".