Pro-Taliban militants and Pakistani soldiers battled for control of a major tribal town during President George Bush's visit to Pakistan this weekend, signalling a worrying decline in security in the semi-lawless tribal areas.

Helicopter gunships, heavy artillery and small arms fire were used during 24 hours of fighting in Miran Shah, the capital of North Waziristan tribal agency. At least 46 militants and five soldiers died, an army spokesman said.

The violence underscored the instability of the tribal belt where several al-Qaida members, possibly including Osama bin Laden, are sheltering among sympathetic Pashtun tribesmen.

"There's a lot of work to be done in defeating al-Qaida," Mr Bush said after meeting President Pervez Musharraf in Islamabad on Saturday.

At about the same time in the town of Miran Shah, 180 miles to the west, gunships were strafing a telephone exchange captured by the rebels, and civilians cowered in their homes as shells whistled overhead. The army trained its guns on a religious school run by an extremist Islamist cleric, Maulana Sadiq Noor. The weekend battle was the worst violence since a 2004 clash in South Waziristan that left more than 600 troops dead.

The fighting eased yesterday, allowing hundreds of residents to flee. The streets were empty, the bazaar deserted and a firebombed bank was smouldering, said a military spokesman, Major General Shaukat Sultan. A local man, Noor Nawaz, told the Associated Press news agency as he fled with his veiled wife and their three children: "People are extremely scared. Nobody has slept. Children were crying."

Gen Sultan said that the violence was sparked by an army ambush on a militant hideout, 10 miles outside Miran Shah, last Wednesday. "We were able to kill over 45 militants and destroy their base," he said.

It was possible civilians had died, he admitted. "Yes, we feel sorry for them. But if their compounds are being used by militants, then we have to respond," he said.

Analysts said the military offensive was probably timed to impress Mr Bush. "It's a little too coincidental," said Samina Ahmed of the International Crisis Group.

A tight security cordon was lifted from Islamabad as opposition politicians who had been detained to prevent them from protesting against the US president's visit were released, including Imran Khan. The former cricket star said he watched Mr Bush meet General Musharraf on television during his house arrest. "Bush came here to weaken democracy by strengthening a dictator," he said. He said that 160 of his supporters had been arrested. "Parts of the country are slipping into civil war because of these policies," he told the Guardian.

The arrests were at variance with comments by Gen Musharraf that he had nurtured "the essence of democracy" in Pakistan. Mr Bush stressed the need for elections in 2007, apparently to quash suggestions by some Musharraf officials that the poll might be postponed.

Several internal conflicts have flared dramatically and tested Gen Musharraf's authority in recent months. Tribal rebels in Baluchistan province have stepped up their attacks on military installations and gas pipelines, inflicting a steady drip of casualties. Eleven soldiers were wounded in an ambush in Pathar Nala village, south-east of Quetta, yesterday.

In southern Afghanistan, meanwhile, a Canadian officer was critically injured after a militant attacked him with an axe during a meeting of village elders. The soldier "had removed his helmet as a sign of respect", said a Canadian military statement.