The attack on the US consulate in the eastern Libyan city of Benghazi came after midnight in a blaze of gunfire and grenade explosions, taking the diplomats inside by surprise.

The consulate IT expert, Sean Smith, was chatting online to fellow internet gamers when it happened. According to another player, who goes by the online handle of The_Mittani, Smith “said ‘fuck’ and ‘gunfire’ and then disconnected and never returned”.

Smith was one of four Americans killed in the Benghazi attack along with the ambassador, Chris Stevens, and two bodyguards who have not yet been named, presumably because they were members of US special forces. CNN quoted a senior US official as saying the four became separated from their colleagues trying to escape to the roof, where they succumbed to smoke inhalation.

A photograph taken in the chaos outside showed Stevens being carried, apparently unconscious, by a crowd of Libyans. He was taken to a Benghazi hospital where a doctor, Ziad Abu Zeid, initially had no idea who the emergency patient was. The doctor told Associated Press that Stevens had “severe asphyxia”, apparently from smoke inhalation, causing stomach bleeding, but had no other injuries. The doctor tried for 90 minutes to revive him but to no avail.

Stevens was only the sixth American ambassador to be killed in the line of duty in the country’s history. His killing is likely to have far-reaching consequences in American attitudes towards the Arab spring, and could become a factor in the US presidential elections.

It was the culmination of a day of rage in north Africa ostensibly triggered by a previously obscure, rabidly anti-Islamic film made a year earlier in the US. Demonstrators stormed the US embassy compound in Cairo, climbing over the outer wall, taking down the flag and daubing the buildings with Islamic slogans. There were suggestions by unnamed senior US officials that the attack in Benghazi had been planned by an extremist group using outrage at the film as a diversion. But it seems likely the anger generated by the movie provided both the occasion and pretext for the lethal assault.

According to al-Jazeera’s correspondent in Benghazi, Suleiman Idrissi, “at about 11.30pm a group of people calling themselves Islamic law [Sharia] supporters heard there would be an American movie insulting the prophet Muhammad. Once they heard this news they came out of their military garrison, and went into the streets calling on people to go ahead and attack the American consulate in Benghazi.”

The long fuse that led to the explosion of violence that ultimately killed Stevens was lit last summer in California, where someone calling himself Sam Bacile set about making what is likely to be remembered as one of the most notorious films in recent history. It is far from clear who Bacile is. He described himself in telephone interviews this week with the Wall Street Journal and AP as an Israeli-American property developer, but neither the Israeli authorities nor the California realtors association had heard his name.

In July, a clip from Bacile’s film, called Innocence of Muslims, a bizarre and amateurish attack on the prophet Muhammad, appeared on the internet. The subject is religion but the style, production values and acting are reminiscent of gonzo, low-budget porn, a vast industry in southern California, where this movie seems to have been made. Rarely has the porn industry produced anything as intentionally offensive as the Bacile film.

Steve Klein, a militant Christian activist credited as a consultant on the film, told The Atlantic online: “He’s not Israeli â€¦ His name is a pseudonym. All these Middle Eastern folks I work with have pseudonyms. I doubt he’s Jewish. I would suspect this is a disinformation campaign.”

The man calling himself Bacile said he had raised a budget of $5m from 100 “Jewish donors”, whom he declined to name, to make the film, which he wrote and directed himself with the aim of demonstrating his belief, as he described it to the Wall Street Journal, that “Islam is a cancer”.

To that end, Bacile got his amateur cast to depict the prophet Muhammad as a feckless philanderer who approved of child abuse. It took three months, 59 actors and about 45 crew. The result was two hours of stumbling dialogue and wooden acting among flimsy sets, and a stream of gratuitous insults aimed at Muslims. It was screened in an almost empty cinema in Hollywood earlier this year. In another age, that would probably have been the end of the story. In the YouTube era, however, it was a bomb primed for detonation.

Bacile posted a 13-minute English-language trailer on YouTube in early July but it was only in the past week that it appears to have caught on of the online sectarian culture wars.

A Florida pastor, Terry Jones, who had triggered protests in the Islamic world for burning the Qur’an and his campaign to stop the construction of a mosque at the site of the 9/11 attacks, promoted the film on his website and announced his intention to broadcast the trailer at his Gainesville church this week.

The film clip was also promoted last week by Morris Sadik, the Egyptian Coptic Christian also based in California, who runs a small, virulently Islamophobic group called the National American Coptic Assembly. It was later denounced by mainstream Copts in Egypt, but it was too late to stop it going viral.

At some point over the summer, a version of the YouTube trailer surfaced with the dialogue dubbed in Egyptian Arabic and the translated clip was picked up by a firebrand Cairo television host, Sheikh Khaled Abdallah, with a record of focusing on perceived threats to Islam. He aired clips from the video on his television show on Saturday, and the same video clips were posted to YouTube on Monday.

As the audience for Innocence of Muslims grew exponentially, militant Islamists called for a mass protest at the US embassy in Cairo. The organisers said they began planning it last week when Sadik began promoting the trailers, but support for the demonstration snowballed after the Sheikh Abdallah programme.

A crowd of some 2,000 is reported to have gathered outside the embassy walls in Cairo on Tuesday. The security services appear to have been caught flat-footed even though the protest had been announced. Most of the diplomats and local staff had left early and a few dozen of the demonstrators were able to scale a wall, take down the stars and stripes and replace it with a black flag. The Egyptian police only managed to evict them in the late evening.

By that time, however, the spark had jumped westwards to the eastern Libyan city of Benghazi. According to al-Jazeera, an extremist militia called Ansar al-Sharia, one of many such armed groups staking out fiefdoms in Libya in the aftermath of Muammar Gaddafi’s fall, heard about the storming of the Cairo embassy and the American film.

Stevens had the misfortune to be making a short visit to Benghazi, and, according to US officials quoted on Wednesday, was probably not the intended target.

Bacile, whoever he is, was reported to have gone into hiding on Wednesday night. He spoke to AP from an undisclosed location, said he had not expected such a furious reaction, and criticised security at US diplomatic outposts. “I feel sorry for the embassy. I am mad,” he said. But Bacile insisted that the movie would help Israel by exposing what he described as Islam’s flaws to the world.