"The problems come from amongst ourselves," he said.
"If we can save Pakistan and Afghanistan from these problems, from extremism ... then such trilateral meetings are meaningful."
Tens of thousands of US and Nato military alliance troops are curently deployed in Afghanistan to combat Taliban fighters.
Across the border in northwest Pakistan, the military is involved in fierce fighting with pro-Taliban forces who control large areas of the Swat valley.
The one-day summit in Tehran ended with Ahmadinejad, Karzai and Asif Ali Zardari, Pakistan's president, signing an agreement to work together on stabilising the region.
"All three nations, by relying on their strengths, good organisation and co-operation, can solve these issues," Ahmadinejad said at the summit's closing session.
"The Tehran Declaration is a serious declaration of comprehensive co-operation towards achieving the interests of all three nations."
'Beacon of hope'
Al Jazeera's Alireza Ronaghi, reporting from Tehran, said that the meeting was an opportunity for Iran to prove to the new US administration that it can play an important role in the region.
"Iran wants to prove to the world that it is a beacon of hope in a region that is emboiled in turmoil and conflict," he said.
"Iran has been relatively stable security-wise ... and it has always favoured regional security pacts over deals and treaties with foreign forces."
Barack Obama, the US president, has said he is committed to a more regional approach to tackling the security situation in Afghanistan and Pakistan, while at the same time making diplomatic overtures to Tehran.
Elahe Mohtasham, a senior research associate at the Foreign Policy Centre, said Iran had stayed out of the security issues of Pakistan and Afghanistan over the last eight years.
"Iran will be able to play a crucial role as border security between these countries is extremely important," she told Al Jazeera.
"But the trade and economic aspects of relations are as important ... and could have side effects, overlaps with each other.
"For example, Iran wants to provide free or subsidised electricity to Pakistan."
Zardari suggested that the three leaders should meet again in Pakistan, but did not set a date for the summit.
He said Islamabad was committed to fighting the menace of "terrorism, extremism and narcotics which has threatened the region".
"We can tackle them through a comprehensive approach," Zardari said.
During the summit, Iran signed a framework agreement to export natural gas to Pakistan, local news agencies reported.
India had been part of the $7bn so-called "peace pipeline" project, but stayed away from talks in September saying it wanted to agree transit costs through Pakistan on a bilateral basis first.
Iran and Pakistan had agreed on a revised price formula and a new price review mechanism in December which updated terms reached in 2006.