Mr Ahmadinejad offered Iran's full support to Afghanistan
Iranian President Mahmoud Ahmadinejad has accused the US of playing a "double game" in Afghanistan after the US used the same term to condemn Iran's role.
Mr Ahmadinejad said the US had "created terrorists and now say they are fighting them", as he appeared with Afghan President Hamid Karzai in Kabul.
US Defence Secretary Robert Gates, who is also in Kabul, has accused Iran of giving the Taliban low-level support.
Later, Mr Karzai flew to Pakistan for talks with another key neighbour.
This is Mr Ahmadinejad's first visit to Afghanistan since both he and Mr Karzai were re-elected last year.
Quentin Sommerville, BBC News, Kabul
This was a joint news conference, but it was President Ahmadinejad who did most of the talking. At times it must have been uncomfortable listening for President Karzai.
America, his main ally in the fight against the Taliban, should leave Afghanistan for good, said the Iranian president. Only then, would the country find peace. It was the United States that was playing the "double game". This hardly sounds like grounds for compromise.
President Karzai is facing pressure from all sides. David Miliband, the UK foreign secretary, says Mr Karzai isn't doing enough to find a political solution. Military operations won't be enough to bring peace, he's expect to say in a speech later on Wednesday. He will say that any solution will need the full support of the country's neighbours.
But, as President Ahmadinejad showed, those neighbours have conflicting interests, and winning that support won't be easy.
At a joint press conference with Mr Karzai, Mr Ahmadinejad rejected the presence of foreign military forces "as a solution for peace in Afghanistan".
He said: "Our policy is full support for the Afghan people and Afghan government and reconstruction of Afghanistan."
Mr Gates, who is in Afghanistan to review the progress of the current Western troop surge against the Taliban, had earlier accused Tehran of "playing a double game" of offering friendship to the Afghan government while at the same time giving "low-level support" and money to the Taliban.
The Taliban are Sunni Muslims and sworn enemies of Shia Iran, which has growing interests and influence, particularly in western parts of Afghanistan.
Mr Ahmadinejad said it was the US that was playing the "double game".
"They themselves created terrorists and now they're saying that they are fighting terrorists," he said.
Mr Ahmadinejad criticised the US for its troops' presence, saying: "Your country is located on the other side of the world, so what are you doing here?"
Mr Ahmadinejad said that terrorism could not be defeated by armies, only by intelligence.
The BBC's Quentin Sommerville in Kabul says that President Karzai said little at the joint conference, but thanked President Ahmadinejad for his support and described Iran as a realistic friend.
Mr Karzai said: "We are very hopeful that our brother nation of Iran will work with us in bringing peace and security to Afghanistan so that both our countries will be secure."
Mr Karzai will later head to Pakistan for key talks
Mr Gates, attending a base in Kabul province on Wednesday where Western troops are training Afghan soldiers, described Mr Ahmadinejad's visit as "certainly bothersome".
He said the US wanted Afghanistan to have good relations with its neighbours but that those neighbours must treat Afghanistan fairly.
He also said US troops might begin to leave Afghanistan before the previously stated withdrawal start date of July 2011, depending on "conditions on the ground".
However, he added: "We should not be too impatient.
"At the end of the day, only Afghans will be able to provide long-term security for Afghanistan."
Mr Karzai later travelled to Pakistan, which has been accused in the past of providing a haven to the Afghan Taliban.
However, it has recently stepped up its drive to arrest Taliban leaders, including alleged second-in-command Mullah Abdul Ghani Baradar.
The BBC's Aleem Maqbool in Islamabad says Mr Karzai will want the leaders extradited, while Pakistan will argue for more involvement in regional strategy, particularly if Western troops do start to leave Afghanistan.
Later on Wednesday, Britain's Foreign Secretary David Miliband is expected to deliver a speech in the US, urging President Karzai to do more to find a political solution to the conflict with the Taliban.
The military effort alone will not be enough to resolve the conflict, he is expected to say, and Afghanistan's neighbours will need to play a central role in securing peace in the country.