WASHINGTON and BENGHAZI, Libya — President Barack Obama branded the killing of the U.S. ambassador to Libya and three other Americans an “outrageous attack” on Wednesday and vowed to track down the perpetrators, while ordering a tightening of diplomatic security worldwide.

The ambassador, John Christopher Stevens, and the other Americans were killed after Islamist gunmen attacked the U.S. Consulate and a safe house refuge in Benghazi on Tuesday night. The attackers were part of a mob blaming America for a film they said insulted the Prophet Mohammad.

The violence in the eastern city, a cradle of Libya’s U.S.-backed uprising against Muammar Gaddafi last year, came on the anniversary of al-Qaida’s attacks on the United States on Sept. 11, 2001. Another assault was mounted on the U.S. embassy in Cairo in which protesters, who included Islamists and teenage soccer fans, tore down and burned a U.S. flag.

U.S. government officials said the Benghazi attack may have been planned in advance and there were indications that members of a militant faction calling itself Ansar al Sharia — which translates as Supporters of Islamic Law — may have been involved.

They also said some reporting from the region suggested that members of al-Qaida’s north Africa-based affiliate, known as al-Qaida in the Islamic Maghreb, may have been involved.

“It bears the hallmarks of an organized attack,” one U.S. official said. However, some U.S. officials cautioned against assuming that the attacks were deliberately organized to coincide with the Sept. 11 anniversary.

The U.S. military is moving two Navy destroyers toward the Libyan coast, giving the Obama administration flexibility for any future action against Libyan targets, according to a U.S. official. The military also is dispatching a Marine Corps anti-terrorist security team to boost security in Libya.

The violence in Benghazi and Cairo threatened to spread to other Muslim countries on Wednesday. A U.S. official said Washington had ordered the evacuation of all U.S. personnel from Benghazi to Tripoli and was reducing staffing in the capital to emergency levels.

Police fired teargas at angry demonstrators outside the U.S. Embassy in Tunisia and several hundred people gathered in front of the U.S. Embassy in Sudan. In Morocco, a few dozen protesters burned American flags and chanted slogans near the U.S. Consulate in Casablanca.

Obama vowed on Wednesday to bring the Benghazi killers to justice. He called the attack “outrageous and shocking” but insisted it would not threaten relations with Libya’s new elected government.

Libyan leader Mohammed Magarief apologized to the United States over an attack.

Obama said he had ordered an increase in security at U.S. diplomatic posts around the globe. A U.S. official said a Marine anti-terrorist security team was being sent to Libya to boost security there.

The attacks could alter U.S. attitudes toward the wave of revolutions across the Arab world, which toppled secularist authoritarian leaders in Egypt, Libya and Tunisia, bringing Islamists to power.

The violence also could have an impact on the closely fought U.S. presidential election campaign.

Mitt Romney, Obama’s rival in the November vote, criticized the president’s response to the crisis. He said the timing of a statement from the U.S. Embassy in Cairo denouncing “efforts by misguided individuals to hurt the religious feelings of Muslims” made Obama look weak as protesters were attacking U.S. missions.

He said it was “disgraceful” to be seen to be apologizing for American values of free speech. Obama’s campaign accused Romney of scoring political points at a time of national tragedy. Obama said Romney has a tendency “to shoot first and aim later.”

Libyan Deputy Interior Minister Wanis al-Sharif said Stevens and another diplomat died as a result of the consulate attack, while the other Americans died in what a Libyan military officer called an intense and highly accurate mortar attack on the safe house.

Ziad Abu Zaid, the duty doctor in the emergency room at Benghazi Medical Centre on Tuesday, said he had treated Stevens.

“He came in a state of cardiac arrest. I performed CPR for 45 minutes, but he died of asphyxiation due to smoke inhalation.”

U.S. officials said Stevens, information technology specialist Sean Smith and one security officer were trapped under fire in the burning consulate building.

The security officer made it outside and returned with help to search for the diplomats, officials said. The searchers found Smith, who was already dead, but were unable to find Stevens amid repeated exchanges of gunfire between Libyan security forces and the attackers over the next several hours.

“At some point in all of this … we believe that Ambassador Stevens got out of the building and was taken to a hospital in Benghazi. We do not have any information on what his condition was at that time,” a senior U.S. official said.

Stevens’ body was later returned to U.S. custody at Benghazi airport, the official said.

Images of Stevens, purportedly taken after he died, circulated on the Internet. One image showed him being carried, with a white shirt pulled up and a cut on his forehead.

The attack raised questions about the future U.S. diplomatic presence in Libya, relations between Washington and Tripoli, and the unstable security situation after Gaddafi’s overthrow.

Witnesses said the mob included tribesmen, militia and other gunmen. Hamam, a 17-year-old who took part in the attack, said Ansar al-Sharia cars arrived at the start of the protest but left once fighting started.

“The protesters were running around the compound just looking for Americans, they just wanted to find an American so they could catch one,” he said.

“We started shooting at them, and then some other people also threw handmade bombs over the fences and started the fires in the buildings,” he said.

“There was some Libyan security for the embassy outside but when the handmade bombs went off they ran off and left.”

Hamam said he saw an American die in front of him in the mayhem that ensued. He said the body was covered in ash.

“Innocence of Muslims,” the film blamed for provoking the violence, was the work of California-based Israeli filmmaker Sam Bacile, according to a report by The Associated Press. But Reuters could not independently confirm his responsibility for the film, or even that Bacile was his real name.

It portrays Mohammad as a fool, a philanderer and a religious fake and in one clip posted on YouTube, he was shown in an apparent sexual act with a woman. For many Muslims it is blasphemous even to show a depiction of the Prophet.

Bacile, who described himself to AP as an Israeli Jew, spoke to the agency from hiding on Tuesday. He said that the film, which cost $5 million to make, was intended as a political statement and was financed by more than 100 Jewish donors.

Egypt’s Coptic Orthodox Church, meanwhile, condemned Copts living abroad who it said had financed “the production of a film insulting the Prophet Mohammad.” About one-tenth of Egypt’s 83 million people are Christian.

Western countries denounced the Benghazi killings and Russia expressed deep concern, saying the episode underscored the need for global cooperation to fight “the evil of terrorism.”

Many Muslim states focused their condemnation on the film and will be concerned about preventing a repeat of the fallout seen after publication in a Danish newspaper of cartoons of the Prophet Mohammad. This touched off riots in the Middle East, Africa and Asia in 2006 in which at least 50 people died.

Afghan President Hamid Karzai called the making of the movie a “devilish act” but said he was certain those involved in its production were a very small minority.

The U.S. Embassy in Kabul appealed to Afghan leaders for help in “maintaining calm” and Afghanistan shut down the YouTube site so Afghans would not be able to see the film.

Gen. Martin Dempsey, chairman of the U.S. military’s Joint Chiefs of Staff, took the unusual step of telephoning a radical Florida pastor, Terry Jones, and asking him to withdraw his support for the film. Earlier provocative acts by Jones, including publicly burning a Quran, had sparked Muslim unrest.

In Egypt, Prime Minister Hisham Kandil called on Washington to act against the film’s makers for stirring up strife but condemned the violence saying that they had “no relation to the [U.S.] government.”

U.S. Secretary of State Hillary Clinton said the attack was the work of a “small and savage group.”

Abdel-Monem Al-Hurr, spokesman for Libya’s Supreme Security Committee, said Libyan security forces came under heavy fire and “were not prepared for the intensity of the attack.”

Libya’s interim government has struggled to impose its authority on a myriad of armed groups that refused to lay down their weapons and often take the law into their own hands.

Security experts say the area around Benghazi is host to a number of Islamist militant groups who oppose any Western presence in Muslim countries.

U.S. ambassadors in such volatile countries as Libya have tight security, usually traveling in well-protected convoys. Diplomatic missions normally are protected by Marines or other special forces.

Tributes poured in to honor Stevens, who said in a video posted on the embassy website of his involvement in the Libyan revolution: “I was thrilled to watch the Libyan people stand up and demand their rights.”

Stevens, who was 52, grew up in California, graduated from Berkeley and worked in North Africa as a Peace Corps volunteer. He taught English in Morocco before joining the foreign service where he worked in the Middle East and North Africa.

The worst-case scenario for Western governments is that the spate of recent unrest in Libya could be the start of an Iraq-style insurgency by Islamist militants. That could hit oil exports as the energy sector depends on foreign workers.

However, security analysts say an insurgency is unlikely to gain the kind of traction it had in Iraq, mainly because Western states have no military presence in Libya.

Additional reporting by Samia Nakhoul in Beirut, Marie-Louise Gumuchian in Tripoli, Hadeel Al Shalchi in Tripoli, Sarah N. Lynch, Arshad Mohammed, Andrew Quinn, Matt Spetalnick, Steve Holland and Mark Hosenball in Washington, and Reuters reporters in Cairo and Benghazi; Writing by David Brunnstrom and Peter Millership.