The leaders said in a joint statement after meeting in the Turkish capital on Monday: "They agreed to deny sanctuary, training and financing to terrorists and to elements involved in subversive and anti-state activities in each other's country and to initiate immediate action on specific intelligence exchanges in this regard."
Pakistan and Afghanistan have accused each other of not doing enough to stop the Taliban, which has increased attacks on US and Afghan forces after the traditional winter lull.
Afghanistan says the Taliban is being run from Pakistan, which Islamabad denies.
Pakistan says that the roots of the fighting lie in Afghanistan and the failure of Karzai's government to provide people with security, improvements in their economic situation and greater representation for ethnic Pashtuns in government.
It was the first meeting between Musharraf and Karzai since September when they were brought together by George Bush, the US president, to try to ease tensions.
In an interview with Turkey's NTV, Musharraf said he hoped a new era had begun.
"We've decided to overcome the basic differences together and that this would be useful for both countries," he said "We've not yet talked about the details."
Pakistan denies continued formal support for the Taliban, which it helped to power in the 1990s, but the issue of cross-border infiltration has soured ties.
Musharraf accused Karzai in a newspaper interview published last week of being weak on "terrorism". Karzai has said the Taliban is getting help from Pakistan.
|"We've decided to overcome the basic differences together and that this would be useful for both countries... We've not yet talked about the details"|
Pervez Musharraf, Pakistan president
About 45,000 US and Nato troops are in Afghanistan battling the Taliban, mostly in ethnic Pashtun-dominated provinces on the border with Pakistan.
Pakistan, which is also battling militants, says it is trying to stop infiltration into Afghanistan. It has deployed about 90,000 troops on the border and is fencing parts of the frontier. Afghanistan opposes the fence because of long-standing disputes over the border.
The two countries, along with Turkey, a traditional ally of both, will establish a joint working group to monitor progress on the measures agreed in Ankara, the statement said.
Turkey offered to organise further meetings later in 2007 and 2008.