Britain pledged to provide Pakistan more equipment to battle the kind of improvised explosives that killed the soldiers in North Waziristan and to share expertise in protecting sporting events. Britain hosted the Olympic Games last summer.
"The enemies of Pakistan are enemies of Britain, and we will stand together and conduct this fight against extremism and terrorism together," Cameron said at a joint news conference with Sharif in the Pakistani capital, Islamabad.
Cameron arrived in Pakistan following a visit to neighboring Afghanistan. He welcomed Pakistan's stated commitment to help promote a peace deal with the Afghan Taliban.
Pakistan is seen as key to any deal because of its historical links with the insurgents. Pakistan pushed the Taliban to carry through with its recent step to set up a political office in the Gulf country of Qatar, although acrimony between the insurgents and the Afghan government has hampered the negotiation process.
"I assure Prime Minister Cameron of our firm resolve to promote the shared objective of a peaceful and stable Afghanistan to which the 3 million Afghan refugees currently living in Pakistan can return with honor and dignity," Sharif said at the news conference.
Sharif has also pushed for peace talks with the Pakistani Taliban, although a series of attacks by the group since he took office in early June have led many to question that approach.
The Taliban claimed responsibility for shooting to death 10 foreign mountain climbers and a Pakistani guide in northern Pakistan a week ago, an attack the group said was retaliation for a U.S. drone strike that killed the Taliban's deputy leader.
The Taliban withdrew their offer of peace talks with the Pakistani government following the drone strike. The government continues to stick by its stance that negotiating with the group is the only way to bring peace.
Critics of talks point out that past peace deals eventually collapsed, offering the militants a chance to regroup. They also note that the Taliban reject Pakistan's democratic government and believe Islamic law should be applied throughout the country.
Khan reported from Peshawar. Associated Press writers Sebastian Abbot and Zarar Khan in Islamabad and Rasool Dawar in Peshawar contributed to this report.