Dozens of Taliban militants armed with guns, grenades and bombs have attacked a prison in north-west Pakistan, freeing more than 250 prisoners, including 25 "dangerous terrorists", according to officials said.

The militants killed six policemen, six Shia Muslim prisoners and two civilians during the attack on Monday night in the town of Dera Ismail Khan, said the town's commissioner, Mushtaq Jadoon. One of the Shias was beheaded, said Jadoon, and 15 policemen were wounded.

Around 70 militants arrived at the prison by car and motorcycle at around 11.30pm and their assault lasted for about four and a half hours until most of the fighters escaped, said intelligence officials.

The attack began with a huge explosion that one resident, Sharafat Khan, said was so loud "it rattled every house in the neighbourhood". The militants then detonated dozens of smaller bombs at different points along the prison walls, causing them to collapse, Jadoon said. They also used rocket-propelled grenades and hand grenades during the attack, he said.

Security forces engaged the attackers, at least eight attackers of whom wore police uniforms and entered the prison on motorcycles adorned with Taliban flags. They used megaphones to call out the names of specific prisoners for whom they were looking.

The militants broke open the cells and freed 253 prisoners, including 25 "dangerous terrorists", said Jadoon.

Pakistani Taliban spokesman Shahidullah Shahid claimed responsibility for the attack, saying 150 militants took part and about 300 prisoners were freed. Eight of the attackers wore suicide vests and two detonated their explosives, Shahid told the Associated Press by telephone from an undisclosed location.

Authorities captured nine prisoners who escaped and were searching for the others, as well as the militants, said Jadoon. Army soldiers were called in as reinforcements.

A curfew had been imposed in Dera Ismail Khan and the nearby town of Tank while the search contented, said Amir Khattak, Dera Ismail Khan's deputy commissioner. The town is located near Pakistan's semi-autonomous tribal region, the main sanctuary for Taliban and al-Qaida militants in the country.

Officials had received a letter threatening an attack on the prison but didn't expect it so soon, said Khalid Abbas, head of the prison department in Khyber Pakhtunkhwa province.

In April 2012 Taliban militants armed with automatic weapons and rocket-propelled grenades battled their way into a prison in the city of Bannu in north-west Pakistan, freeing close to 400 prisoners, including at least 20 described by police as "very dangerous" insurgents.

One of the militants freed in that attack, Adnan Rasheed, recently gained attention by writing a letter to teenage education activist Malala Yousafzai, who was shot in the head by the Taliban in 2012. Rasheed said he wished the attack hadn't happened but told Malala that she had been targeted for speaking ill of the Taliban.