(CNN) -- President Barack Obama said he discussed the attacks against U.S. diplomatic offices with his Afghan counterpart, and both leaders are committed to ensuring the violence does not spread to Afghanistan.
Crowds stormed the U.S. Consulate in Benghazi, Libya, and demonstrated at the U.S. Embassy in Cairo, protesting the online release of a film they believe mocks Islam.
The film, produced in the United States, depicted the Muslim Prophet Mohammed as a child molester, womanizer and ruthless killer.
The Benghazi raid Tuesday night killed the U.S. ambassador to Libya, Chris Stevens, and three other Americans.
In a statement Wednesday that did not mention the killings, Afghan President Hamid Karzai angrily condemned the film, describing it as an affront to Islam.
"Insult to the greatest Prophet of Islam means insult to high values of 1.5 billion Muslims across the world," Karzai's office said in a statement. "This offensive act has stoked interfaith enmity and confrontation, and badly impacted the peaceful coexistence between human beings."
His statement neither appealed for peace nor denounced the killings, sparking fears that his focus on the film could spark protests in his nation.
Afghanistan has a history of anti-U.S. protests. In February, the country plunged into chaos after American troops mistakenly burned copies of the Quran at Bagram air base.
At the time, Obama apologized to Karzai, calling the burning an inadvertent error.
In 2010, Karzai condemned Florida pastor Terry Jones for burning Qurans. His outcry fueled an already tense situation, prompting mob killings in the northern city of Mazar-e-Sharif.
Analysts said the outrage has the potential to spill over to Afghanistan.
"People are boiling with anger with the United States because they hosted the director of the movie," said Abdel Bari Atwan, author of "The Secret History of al Qaeda."
Relations between the United States and the Afghan leader are not at their best, according to the author.
"Karzai has to be careful with his words because he does not want to be a target of angry Muslims. He knows Afghans are frustrated by the behaviors of U.S. soldiers there."
In addition to Karzai, Obama spoke with the leaders of Libya and Egypt, the White House said.
During his call with Obama, Karzai "expressed condolences for the tragic loss of American life," and both leaders highlighted the importance of ensuring that the violence does not extend to U.S. forces or Afghans.
The two presidents "discussed the importance of working together to help ensure that the circumstances that led to the violence in Libya and Egypt do not pose a threat to U.S. forces or Afghans," the White House said.
Egyptian President Mohamed Morsy also expressed condolences for the killings in Libya and said Egypt will ensure that American personnel in the nation are protected.
"This is an act that we reject, and such an act is rejected by Islam as well," Morsy said in a recorded video message from Brussels broadcast by Nile TV.
"Those who are attacking the embassies do not represent any of us," he said. "At the same time, on my behalf and on behalf of the entire Egyptian people, I'd like to issue my sincere condolences and my deepest concerns, and I strongly condemn the killing of the American ambassador in Benghazi and those who were with him."
The Taliban issued a statement denouncing the "violations against our heavenly book."
U.S. officials said the four-hour assault in Libya on Tuesday night seemed to have been planned, and the heavily armed attackers used the protest as a diversion.
Sources tracking militant Islamist groups in Libya said a pro-al Qaeda group is a chief suspect. The group is responsible for a previous armed assault on the Benghazi consulate, sources said.
Stevens was the victim of a targeted al Qaeda revenge attack, London think tank Quilliam speculated Wednesday.
The assault "came to avenge the death of Abu Yaya al-Libi, al Qaeda's second in command killed a few months ago," it said.
Libyan officials denounced the attacks, and apologized to the United States and the world.
Pakistan, China and Turkey also condemned the violence, with the latter's Foreign Ministry calling on Libya to ensure security for diplomats stationed in the nation.
In an editorial, Chinese state media called on the U.S. to rethink its Middle East policies.
"On one hand, this tragedy shows the vulnerable security and intense conflicts in Libya," the state-run Xinhua news agency said. "On the other hand, the Libyans are not grateful to the U.S. for 'freeing them,' which they didn't ask for."