Swedish police said they arrested a fifth suspect.
The Danish Security and Intelligence Service, known as PET, said three of the four men were residents of Sweden and had entered the country during the night of Tuesday to Wednesday. The fifth suspect was a 37-year-old Swedish citizen of Tunisian origin living in Stockholm.
Jakob Scharf, the head of PET, said the arrests in Denmark were made after close cooperation with Swedish police.
"An imminent terror attack has been foiled," Scharf said, describing some the suspects as "militant Islamists." He said that more arrests could not be ruled out.
Scharf said they considered the attack had been thwarted and that "there was no need to raise the terror threat alert level" in Denmark.
PET said the group had been planning to enter the building where the Jyllands-Posten daily has its Copenhagen newsdesk and had wanted "to kill as many of the people present as possible."
The other three suspects living in Sweden were a 44-year-old Tunisian citizen, a 29-year-old Lebanese-born man and a 30-year-old whose origin was not immediately known. The final suspect was a 26-year-old Iraqi asylum-seeker living in Copenhagen, PET said.
The four men face preliminary charges of attempting to carry out an act of terrorism. They will face a custody hearing Thursday.
The men were arrested in Greve, south of Copenhagen, and Herlev, west of the Danish capital. During the raids, police found an automatic weapon, a silencer and live ammunition, PET said.
Danish Justice Minister Lars Barfoed described the terror plans as "terrifying."
"The group's plans to kill as many as possible is very frightening and is probably the most serious terror attempt in Denmark," Barfoed said.
The head of Sweden's security police, Anders Danielsson, said that "it has been possible to avert a serious terror crime in Denmark through efficient and close cooperation between PET and the (Swedish) security police."
Danielsson said the suspects who are residents in Sweden are also being investigated for suspected terror crimes in that country.
There have been at least three attacks against the Danish newspaper or the artist who drew the most contentious of 12 cartoons, published by the daily in 2005 as a challenge to perceived self-censorship.
The drawings plunged Denmark into turmoil in early 2006 with massive and violent protests in Muslim countries where demonstrators said the drawings had profoundly insulted Islam.
Islamic law generally opposes any depiction of the prophet, even favorable, for fear it could lead to idolatry.
Associated Press Writer Louise Nordstrom contributed to this report from Stockholm.