Iranian President Mahmoud Ahmadinejad used a brief visit to Afghanistan on Wednesday to lob insults at the United States and argue that international forces won't stop terrorism and will only lead to more civilian deaths.

Ahmadinejad said the United States was playing a "double game" in Afghanistan, fighting militants it once supported.

His comments were a retort to U.S. Defense Secretary Robert Gates who earlier in the week accused Tehran of "playing a double game" by nurturing relations with the Afghan government while supporting insurgents to undermine U.S. and NATO troops. Tehran denies the allegation.

Ahmadinejad threw back the phrase Wednesday.

"I believe that they themselves," who are now fighting militants in Afghanistan, "are playing a double game," said Ahmadinejad, who has made several visits to the country. "They themselves created terrorists and now they're saying that they are fighting terrorists."

During the 10 years that the Soviet Union fought in Afghanistan, the U.S. supplied rebels with everything from mules to advanced weaponry, including Stinger anti-aircraft missiles that helped neutralize Soviet air power. After the U.S. money evaporated, the world watched Afghanistan plunge into chaos and eventually harbor the Al Qaeda terrorist network responsible for the Sept. 11 attacks.

While the United States has long-running suspicions about Iran's nuclear program, the two nations view the Taliban as a common enemy. Iran believes that the Taliban warped the Islam religion to suit its ideology. Taliban forces killed eight Iranian diplomats in the late 1990s, and the militant group makes money from drug-smuggling operations across Iran's border with Afghanistan.

But while Iran supported efforts to oust the repressive Taliban regime from power in Afghanistan, it opposed the U.S.-led offensive to topple the Taliban after the 2001 terrorist attacks. Iran is wary of a long-term U.S. military presence in the region.

Ahmadinejad spoke at a joint news conference with Afghan President Hamid Karzai at his presidential palace, but it was the Iranian leader who did nearly all of the talking. He even took an additional question from a reporter after Karzai had ended the conference, and then continued his litany of complaints against the United States.

"Your country is located on the other side of the world so what are you doing here?" Ahmadinejad asked the reporter from a U.S. media outlet.

Ahmadinejad criticized the West, saying its policies were resulting in Afghan civilians being killed and that its money spent on troops would be better spent on irrigation and other development projects.

"Those who say they are fighting terrorists, they are not successful," he said.

The top commander in Afghanistan, U.S. Gen. Stanley McChrystal, has ordered troops to use airstrikes judiciously and fire cautiously to reduce civilian casualties. Still, each report of civilians killed unleashes raw emotions that highlight a growing impatience with coalition forces' inability to secure the nation after more than eight years of war.

Ahmadinejad's criticism of the U.S. put Karzai in an awkward spot because he is dependent on the U.S. and other donor nations to rebuild Afghanistan after decades of war. When the news conference was ended a second time, Ahmadinejad apologized to Karzai for talking so long.

In his opening remarks, Karzai struck a friendly chord. But just in case the Afghan media didn't share his collegial approach, security guards hurriedly collected water bottles from journalists just before the news conference started. Journalists in the audience said it was done to prevent anyone from throwing a bottle at the Iranian leader.

"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," Karzai said. He said Afghanistan had a very good relationship with Iran and he thanked the Iranian people for aid used for reconstruction.

But Karzai also displayed Afghanistan's fierce independence, saying, "We have mentioned several times to our brother nation Iran that we don't want any one to use our soil against any of our neighbors."

Abdul Hameed Mubrez, a political analyst and former Afghan minister, said Ahmadinejad should respect Afghanistan's positions.

"We reject this kind of interference in our policy," Mubrez said. "Ahmadinejad wants to disrupt Afghanistan's good relations with the United States, but he won't succeed. We want to continue our friendly relations with the United States and the European countries."

Gates, who left Afghanistan shortly before Ahmadinejad spoke, said the United States wants Afghanistan to have good relations with its neighbors, but that they need to deal fairly with the Karzai government.