President Barack Obama has vowed to bring to justice those who carried out the attack that killed the US ambassador to Libya.
But he said the US consulate attack in Benghazi would not harm ties between the US and the new Libyan government.
It has sparked a political row in the US, with rival Mitt Romney criticising Mr Obama before the president hit back.
Ambassador J Christopher Stevens died after gunmen stormed the consulate amid protests over an anti-Islamic film.
Meanwhile, protesters in Egypt have clashed with security forces after crowds returned to the US embassy in Cairo for a second evening of demonstrations over the film.
Riot police fired tear gas at protesters, some of whom were seen carrying petrol bombs, witnesses said.
US officials said the consulate compound in Libya began taking heavy fire around 22:00 local time on Tuesday night, and the main building was in flames soon afterwards.
Three other Americans were also killed, including Sean Smith, a state department employee, in what the White House described as a "complex" attack.
Libyan and US security forces tried to retake the compound several times, US officials said, but only succeeded early on Wednesday. Mr Smith was found dead inside the compound.
US officials said Washington was investigating whether the attack was organised in advance, rather than a spontaneous assault sparked by demonstrations over the film.
Officials told Reuters there were suspicions that a militia known as the Ansar al-Sharia brigade was involved in the attack. The group has denied the claim.
They also cited reports suggesting al-Qaeda's north Africa-based affiliate, known as al-Qaeda in the Islamic Maghreb, may have been involved, the news agency reports.
The protests followed rallies in Cairo, where demonstrators angry at the film, called Innocence of Muslims, breached the walls of the US embassy and tore down the flag.
Little is known with any certainty about the origins of the film, including about a man named as Sam Bacile, reported as being behind its production.
BBC reporters probing his background on Wednesday were unable to confirm personal details.
A US marine anti-terrorism team is being sent to Libya to bolster security after the attack, a US defence source told reporters in Washington.
Speaking in the Rose Garden at the White House, President Obama told reporters: "Justice will be done."
He condemned "in the strongest possible terms the outrageous and shocking" attack.
"It is especially tragic that Chris Stevens died in Benghazi because it is a city that he helped to save," he added, praising the dead ambassador for his work in Libya after the overthrow of the late Col Muammar Gaddafi.
Earlier, the president was criticised by his Republican election rival, Mitt Romney, who said the administration appeared to "sympathise with those who waged the attacks".
According to Mr Romney, Mr Obama's team had sent "mixed signals to the world" in the face of violence, referring to a statement from the US embassy in Cairo, issued before it was known Mr Stevens had been killed.
Mr Romney stood by his criticism of the administration as events unfolded on Wednesday, despite a lack of firm support from his Republican party.
Later, in an interview with CBS on Wednesday, Mr Obama said that his election opponent had "a tendency to shoot first and aim later".
"It's important for you to make sure that the statements that you make are backed up by the facts."
Protests against the violence and against extremism in general were also held in Tripoli and Benghazi.
The deadly violence was condemned by Libyan officials.
Libya's interim leader, Mohammed Magarief, apologised to the US over the killings, which he called "cowardly criminal acts". Libya's deputy envoy to the UN, Ibrahim Dabashi, promised an investigation.
"We cannot understand how this group, or these persons, could have eliminated such a wonderful person," he told the Security Council.
Correspondents say the film at the heart of the row, which appeared on YouTube translated into Arabic, is highly provocative and insulting to Muslims. An Islamic tenet bans the portrayal of the Prophet Muhammad.
In other developments on Wednesday:
- Nigeria placed its police force on red alert
- The US embassy in Algiers warned Americans in Algeria to avoid non-essential travel
- Tunisian police fired tear gas and rubber bullets into the air to disperse a protest near in Tunis
- Demonstrations were reported in Khartoum, Sudan, the US consulate in Casablanca, Morocco, and at UN offices in Gaza
- Afghanistan ordered a block on YouTube until the offending film was removed - but the site was still visible to users in Kabul