Image copyright AP Image caption Grief-stricken relatives rushed to the hospital when news of the bus attack broke

At least 45 people have been killed and 13 injured in a gun attack on a bus carrying Ismaili Shia Muslims in the Pakistani city of Karachi, police say.

Police said six gunmen on motorcycles had stopped the bus and fired indiscriminately at passengers.

Prime Minister Nawaz Sharif has condemned the attack and ordered an investigation.

Pakistani Taliban splinter group Jundullah and Islamic State (IS) both said they had carried out the attack.

Later, the militant group Tehrik Taliban Pakistan (TTP) also said it was behind the shooting.

Analysts say Pakistan's fragmented militant movements have previously competed to claim responsibility for attacks.

Wednesday's attack was the second deadliest in Pakistan this year after 62 Shia Muslims were killed in a suicide bombing in January.

Media playback is unsupported on your device Media caption Gunmen boarded the bus and fired indiscriminately, says Shahzeb Jillani

Analysis: M Ilyas Khan, BBC News, Islamabad

This is the first such attack on the Ismaili community in Pakistan, and it is shocking even by the standards of Karachi. The sheer numbers of casualties and the manner in which they were killed provide an uneasy contrast with the peaceful image of this Shia sect, which makes up a tiny proportion of Pakistan's mostly Sunni Muslim population.

We are not likely to find out for sure who did it because Karachi is in the throes of an armed struggle between the military and a confusing array of political, religious and sectarian militants.

Ismailis consider the Aga Khan their hereditary spiritual guide. They mostly inhabit the Himalayan region of northern Pakistan, but also have a significant presence in Karachi where they run businesses and charities, and tend to use community-built accommodation and transport.

The attack is likely to cause some diplomatic tensions for Islamabad as the Aga Khan has been a major source of development funds. This may be one reason why both the prime minister and the army chief have set aside other engagements to head for Karachi.

About 60 people were on the bus when it was stopped in the Safoora Chowk area on its way to the Ayesha Manzil Ismaili centre, police said.

Relatives of some survivors said the gunmen were dressed as policemen.

"As the gunmen climbed on to the bus, one of them shouted, 'kill them all!'," a survivor told Pakistani TV.

"Then they started indiscriminately firing."

They said the bus driver was killed and an injured passenger drove the bus to Memon Hospital Institute.

BBC Urdu service editor Aamer Ahmed Khan in Karachi said the bus was making one of five daily scheduled trips between a gated community housing mainly Ismailis on the north-eastern outskirts and the main city.

Who are the Ismailis?

Ismaili Shias, in common with other Shia Muslims, revere Ali, son-in-law of the Prophet Muhammad, but they also revere the Imam Ismail who died in 765 AD

They interpret the Koran symbolically and allegorically

There about 15 million Ismailis in more than 25 countries, including 500,000 in Pakistan

Spiritual leader Prince Karim Aga Khan is a philanthropist and business magnate. He gives his name to bodies including a university, a foundation, and the Aga Khan Program for Islamic Architecture at Harvard University and the Massachusetts Institute of Technology

He has encouraged Ismailis settled in the industrialised world to contribute to those communities

Image copyright EPA Image caption The attack seems to have been well planned

One man at the hospital told AFP news agency: "I have come to collect the body of my young son. He was a student preparing for his first year exams at college."

Ismaili spiritual leader Prince Karim Aga Khan said in a statement the "attack represented a senseless act of violence against a peaceful community".

Amnesty International said the attack marked "a new low in a campaign of sectarian violence that has left Pakistan's religious minorities fearing for their lives".

It said extremists in the country were operating with impunity.

Mr Sharif described the killings as "a deplorable attempt to spread chaos".

A Jundullah spokesman said the group had carried out the attack because it considered the victims to be infidels. He threatened more attacks against Ismailis, Shias and Christians.

A statement purportedly from IS posted on jihadist Twitter accounts also claimed the attack.

Pakistan's army chief Gen Raheel Sharif has cancelled a trip to Sri Lanka and is leading the investigation, the BBC's Urdu editor said.

Pakistan is about 20% Shia and 70% Sunni, with the remainder made up by other minorities. Karachi, Pakistan's commercial capital, has long had a reputation for sectarian violence.