David Cameron has vowed that Britain will "stand together" with Pakistan in the fight against terrorism.
The UK prime minister told Pakistan's new prime minister, Nawaz Sharif, that his country has a vital role to play in transforming Afghanistan from a war-torn state into a stable society.
Cameron is the first foreign leader to visit Pakistan since its first democratic elections last month.
In talks in Islamabad, Sharif hailed British Pakistanis for being "a living bridge" between the two countries which benefited Pakistan.
Pakistan has struggled to curb extremists using its borderlands as a base and its security services have been accused of aiding extremists.
Cameron said, "The friends of Pakistan are friends of Britain; the enemies of Pakistan are enemies of Britain. We will stand together and conduct this fight against extremism and terrorism together."
On Afghanistan, Cameron said, "I profoundly believe that a stable, prosperous, peaceful, democratic Afghanistan is in Pakistan's interests - just as a strong, stable, peaceful, prosperous and democratic Pakistan is in Afghanistan's interests.
"I know that you and President Karzai will work together towards those ends."
Defeating extremism called for a range of policies focusing on education and eradicating poverty, Cameron cautioned.
Britain will invest £3 billion in Pakistan by 2015 as part of its commitment to international development, Cameron said.
He said, "This is a battle that, yes, requires a tough and uncompromising security response, but it is also a battle that has to go so much wider."
Sharif responded, "We hope that the UK will continue these efforts to seek sustainable peace and stability in Afghanistan. We believe that such a process should be inclusive, Afghan-owned and Afghan-led."
The talks on Sunday 30 June followed meetings between Cameron and Pakistan's President Asif Ali Zardari the previous day.
Sharif is a veteran politician, having served two previous terms as PM, from 1990-1993 and 1997-1999, when he was deposed in an army coup led by General Pervez Musharraf.
He was jailed and fled into exile in Saudi Arabia before returning to Pakistan in 2007.
Sharif won a surprise landslide victory in Pakistan's general elections in May.