Afghanistan and Pakistan say they are determined to stamp out militants mounting attacks on their shared border.
The joint pledge came after a meeting between Afghan President Hamid Karzai and Pakistani President Pervez Musharraf.
The leaders were in general agreement
It was part of a two-day state visit by the Afghan leader to Pakistan, his first for almost a year.
The talks come at a time when fugitive militants, believed to be connected to the ousted Afghan Taleban, have been accused of carrying out attacks from Pakistan's tribal areas.
Mr Karzai said the two countries needed to work together.
We want terrorists, whether in Afghanistan or in Pakistan,
arrested and tried
Afghan President Hamid Karzai
"The greatest challenge that we face in Afghanistan is the continuation of terrorism, and their presence along the border of Afghanistan and their occasional activities inside Afghanistan," he told reporters on Wednesday at the end of his trip.
Earlier, Mr Musharraf had shared those concerns and said: "We will fight terrorism all the way.
"We are mutually complementing each other to tackle the problems."
Overnight, suspected Taleban fighters exchanged fire with Afghan forces across the border between the two countries.
The exchange is reported to have continued for several hours.
Officials in southern Afghanistan say militant incursions from Pakistan are on the rise.
Khalid Pashtoon, a spokesman for the governor of Kandahar, told the BBC that before the Taleban were overthrown the mujahideen had been prevented from accessing Afghanistan.
Mr Pashtoon said: "Now, the same Taleban are easily accessing or crossing the border into Afghanistan and creating a lot of trouble."
Pakistan denies the claims and President Karzai said he had now been reassured that Pakistan was not supporting militant groups who are believed to be launching attacks on Afghan and US forces.
Afghan and US troops have been targeted by militants
During his trip, Mr Karzai also met Pakistan Prime Minister Zafarullah Jamali and discussed worsening border security, the repatriation of Afghan refugees and the release of Pakistani prisoners from Afghan jails.
Last week Pakistani militiamen and Afghan forces exchanged heavy fire over a disputed village of Ghulam Khan on the Durand line - the permanent boundary that marks the border between the two countries.