BERLIN — 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 intent of carrying out a mass shooting motivated by hatred of Muslims.

Mohammad Rafiq said he threw the gunman to the ground after the man 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 gunman.

Rafiq’s quick action helped avert an attack that brought back painful memories of the Christchurch shootings in New Zealand 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 station chief Rune Skjold said in a statement. “These persons showed great courage.”

The attempted attack on the al-Noor center 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 al-Noor mosque preparing for the festivities.

Authorities are 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.

A suspect, whose exact age has not been released but who has been listed as being 21 or 22, appeared in court with bruises across his face. He has been charged with homicide and terrorist acts. Following the attempted attack on the mosque, investigators found the body of his 17-year-old stepsister at a different location.

Rafiq’s lawyer, Abdul-Satar Ali, told The Washington Post in a telephone interview his client suffered injuries on his head, hand and eye while subduing the suspect.

“I suddenly heard shooting from outside,” Rafiq told the Reuters news agency. “He started to fire towards the two other men.”

Rafiq said he used momentum to tackle the shooter, 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 gunman was wearing body armor and a helmet, according to the BBC. 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 be opened only with a code. But it was unclear whether those measures had helped to interrupt the attack.

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

The suspect’s lawyer, Unni Fries, said in a brief phone interview Monday that she could not comment on the case. According to Fries, her client lived in Oslo and was born in 1997.

In a statement Sunday, Oslo police said “the aggressor refused to give any statement to the police this 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 a manifesto, the Christchurch attacker wrote that he was inspired by Breivik.

Read more:

Christchurch endures as extremist touchstone, as investigators probe suspected El Paso manifesto