HERAT, Afghanistan — The departing American ambassador to Afghanistan, Karl W. Eikenberry, lashed out at President Hamid Karzai on Sunday in a carefully calculated and candid rebuke of the Afghan leader’s increasingly inflammatory criticism of the coalition forces.

“When Americans, who are serving in your country at great cost — in terms of life and treasure — hear themselves compared with occupiers, told that they are only here to advance their own interest and likened to the brutal enemies of the Afghan people,” the ambassador said, “my people, in turn, are filled with confusion and grow weary of our effort here.”

The admonition came the day after Mr. Karzai declared that NATO-led coalition forces were “here for their own purposes, for their own goals, and they’re using our soil for that.” Last month, he threatened to denounce the coalition as occupiers if they did not stop attacks on Afghan homes.

Mr. Karzai also said Saturday that the United States had been negotiating with the Taliban. The American defense secretary, Robert M. Gates, acknowledged those talks publicly for the first time on Sunday, saying that the discussions were preliminary.