At least 31 people have been killed and 169 injured in an explosion at a mosque during Friday prayers in Pakistan's capital, Islamabad, officials say.

According to police, a suicide bomber detonated a device after approaching the gates of the Shia mosque in the Tarlai area of the city. Eyewitnesses have told the BBC the blast was preceded by the sound of gunfire.

An emergency was declared as hospitals began to receive the wounded, with an appeal sent out for blood donations.

Condemning the incident, Pakistan's Prime Minister Shehbaz Sharif expressed deep grief as he called for a thorough investigation and the immediate identification of those behind the attack.

In separate statements, Pakistan's President Asif Ali Zardari said targeting innocent civilians is a crime against humanity, while Defence Minister Khawaja Asif suggested India and Afghanistan were behind the attack.

Asif said in a social media post that it had been proven the attacker had travelled to and from Afghanistan, and collusion between India and Afghanistan is being revealed.

Afghanistan's Taliban government described Khawaja Asif's comments as regrettable, saying the Pakistani minister hastily attempted to attribute the incident to Afghanistan without presenting any credible evidence.

Afghan foreign ministry spokesman Abdul Qahar Balkhi earlier condemned the attack and extended condolences, calling such attacks a contradiction to Islamic and humanitarian values.

The Indian foreign ministry also condemned the attack and sent condolences for the loss of life, calling the accusations it may be involved baseless.

Talal Chaudhry, Pakistan's minister of state for interior, said the suspect was not an Afghan citizen, according to local media.

A spokesperson for United Nations Secretary-General Antonio Guterres said he condemned the attack in the strongest terms and called for those responsible to be identified and brought to justice.

'We bowed - then there was an explosion'

Zaheer Abbas was inside the Khadijatul Kubra Mosque praying when he first heard the sound of bullets. After that, we bowed and then prostrated, and then there was an explosion, he told BBC Urdu. After which there were injured people lying everywhere.

Pictures from the mosque in the aftermath show footwear scattered across the floor, shattered glass, and people sat among the wreckage.

Abbas, who said he lost his senses in the moments after the explosion, was eventually pulled out by emergency services and taken to hospital, where he was quickly released. Fortunately, I did not suffer much injury, he said.

According to Pakistan's defence minister, the attacker opened fire and detonated the bomb after being challenged by security guards.

The mosque's caretaker Syed Ashfaq, who lives next door, also heard gunfire, and began running towards the mosque to help. By the time I reached it there had already been an explosion, he told the BBC. Bodies were lying everywhere, some were missing arms, some missing legs. We took the most injured in our own vehicle [to hospital].

Islamabad's deputy commissioner Irfan Memon said on X that the death toll stood at 31, with the injured numbering 169.

Pictures from local hospitals show the injured being brought by private vehicles and ambulances, with one photographer for news agency AFP saying they saw dozens of people arriving at hospital.

Attacks are relatively rare in the capital, although one in November left 12 dead. Another 27 were injured in the blast, outside a courthouse.

The blast on Friday comes after a week of violence which saw 58 civilians killed in coordinated attacks in the province of Balochistan. The Balochistan Liberation Army (BLA) rebel group said it was behind the attacks. Last year was also Pakistan's deadliest in over a decade as combat-related deaths surged 74%, with militants accounting for more than half the death toll, according to a new report released by an independent think tank.