THE US ambassador to Libya and three guards have been killed by protesters angry over a film that ridiculed Islam's Prophet Mohammed.

Ambassador Chris Stevens, 52, died as he went to the Benghazi consulate to try to evacuate staff as the building came under attack by a mob firing machine-guns and rocket-propelled grenades.

The attack came hours after Egyptian protesters climbed the walls of the US Embassy in Cairo, pulling down the American flag and temporarily replacing it with a black Islamic banner.

The film which caused the unrest was produced by Californian property developer Sam Bacile who has now gone into hiding.

He claimed to be both American and Israeli, although Israel said it had no record of him as a citizen. The film was being promoted by an extreme anti-Muslim Egyptian Christian campaigner in the United States. Excerpts from it dubbed into Arabic were posted on YouTube. The video depicts Mohammed as a fraud, a womaniser and a madman in an overtly ridiculing way, showing him having sex and calling for massacres.

Mr Bacile said he had not expected it to cause such a furious reaction. But he remained defiant, saying Islam was a cancer and that he intended his film to be a provocative political statement condemning the religion.

He said it exposed Islam's flaws to the world. "Islam is a cancer, period," he said.

The assaults - the first on US diplomatic facilities in either country - underscored the lawlessness that has taken hold in both Egypt and Libya after revolutions overthrew their autocratic secular regimes and upended the tightly controlled police state in both countries. Islamists, who were long repressed under the previous regimes, have emerged as a powerful force but new governments in both nations are struggling to achieve stability.

Egypt's police, a one-time hated force blamed for massive human rights abuses, have yet to fully take back the streets after Hosni Mubarak's ouster in February 2011.

The uproar over the film also poses a new test for Egypt's new Islamist president, Mohammed Morsi, who has yet to condemn the riot outside the US Embassy in Cairo or say anything about the offending film. The protest was by mostly ultra-conservative Islamists.

Ultra-conservative Islamists also were suspected of being behind the Benghazi attack. Advocating a strict interpretation of Islam, they have bulldozed Sufi shrines and mosques that house tombs in the Libyan capital, Tripoli, and other cities, including ancient sites dating back to 5000 years ago.

Heavily armed, groups like Ansar al-Shariah, or Supporters of Shariah, have claimed responsibility for the attacks on the shrines, declaring Sufi practices as "heretical."

Libya has been also hit by a series of recent attacks that served as evidence of the deep and persistent security vacuum in the country after the fall of Muammar Gaddafi's regime, which was ousted by rebels backed by a Nato air campaign. Many Libyans believe that unrest in their country is in part the work of Gaddafi's loyalists who want to undermine efforts to rebuild the country after last year's ruinous civil war.

Mr Stevens was a career diplomat who spoke Arabic and French and had already served two tours in Libya, including running the office in Benghazi during the revolt against Gaddafi. He was confirmed as ambassador to Libya by the Senate earlier this year.