A 65-year-old former Pakistani military officer is being credited with thwarting an attack at a mosque in Norway, after he tackled a heavily armed gunman who allegedly stormed into the house of worship with the intention of carrying out a mass shooting motivated by hatred of Muslims.

Following the attempted attack on the mosque, investigators found the body of the gunman's 17-year-old stepsister at a different location.

Mohammad Rafiq said he threw the suspect to the ground after the gunman entered the al-Noor Islamic Center in Baerum near the Norwegian capital of Oslo on Saturday, before the two other men inside the mosque rushed to help him pin down the man.

Rafiq's quick action helped avert an attack that brought back painful memories of the Christchurch shootings in New Zealand earlier this year, when a gunman attacked two mosques and killed 51 people during Friday prayers.

"There is no doubt that the swift and firm response from the persons inside the mosque stopped the aggressor," acting Police Chief Rune Skjold said. "These persons showed great courage."

The attempted attack on the al-Noor mosque happened a day before Muslims around the world celebrate Eid al-Adha, one of the most important holidays on the Islamic calendar marking the end of the hajj pilgrimage. Rafiq and the two others had been at the mosque preparing for the festivities.

Authorities are now treating the attack as a suspected act of terrorism, and Norwegian Prime Minister Erna Solberg has condemned Saturday's incident as a "direct attack on Norwegian Muslims." "It was an attack on religious freedom," she wrote on Facebook.

The suspect, Philip Mans­haus, 21, appeared in court with signs of his struggle still visible in the dark bruises under both eyes and scratches on his face and neck. He has been charged with homicide and terrorist acts.

In a closed-door hearing, he did not admit guilt and asked to be set free, his lawyer, Unni Fries, told the Associated Press. The court ordered him held in pretrial detention for four weeks, two of which will be in solitary confinement.

Rafiq's lawyer, Abdul-Satar Ali, said that his client suffered injuries on his head, hand and eye while subduing the suspect.

"I suddenly heard shooting from outside," Rafiq told Reuters. "He started to fire toward the two other men."

Rafiq said he used momentum to tackle the shooter and throw him to the ground and disarm him, before the other men present at the mosque rushed to his help.

"He put his finger inside my eye, up to here; full finger inside my eye," added Rafiq, who said he used to be a Pakistani Air Force officer.

Rafiq said the suspect was carrying multiple firearms and the mosque's director added that the suspect was wearing body armor and a helmet. Authorities are yet to publicly state how many shots were fired and what types of weapons were used.

Ali said the mosque had recently been outfitted with extra security measures, including a front door that can only be opened with a code. But it was unclear if those measures helped to interrupt the attack.

Norwegian authorities said on Sunday that the suspect had "expressed hostile attitudes against immigrants" and were investigating the authenticity of reports that the suspect had announced the attack online, in a post that appeared to include references to both the Christchurch and the El Paso, Texas, shooters.

Fries said her client lived in Oslo and was born in 1997.

In a statement on Sunday, Oslo police said "the aggressor refused to give any statement to the police that night, but new questionings will be carried out."

Norway was witness to one of the deadliest mass shootings in recent years — and one that has inspired others with anti-immigrant views. Anders Breivik, who espoused white-nationalist views, killed 77 people more than eight years ago in attacks in Oslo and at a youth summer camp.

In his manifesto, the Christchurch attacker wrote that Breivik had inspired him.