At least an hour before the assault began, a stream of cars was seen moving toward the U.S. Consulate in the eastern Libyan city of Benghazi. By late Tuesday evening, as many as 50 heavily armed militants had gathered outside its high walls.

They joined protesters outside the consulate who were demonstrating against an American movie that they believed denigrated the prophet Muhammad. But according to one witness, the new arrivals neither chanted slogans nor carried banners.

“They said, ‘We are Muslims defending the prophet. We are defending Islam,’ ” Libyan television journalist Firas Abdelhakim said in an interview.

The gunmen soon opened fire, entered the compound and set the consulate’s buildings aflame. Hours later, the compound was overrun and four Americans were dead. Among them were Ambassador J. Christopher Stevens, 52, and Sean Smith, a State Department employee.

The chaotic scene was described by senior Obama administration officials, Libyan government officials and witnesses. Details about the attack were still emerging late Wednesday. Key facts remain unclear, particularly how Stevens died and how his body wound up at a Benghazi hospital.

Even as evidence was being assembled, the early indications were that the assault had been planned and the attackers had cannily taken advantage of the protest at the consulate.

“Was this a spontaneous act of violence, was this capitalizing on the opportunity posed by [a protest], or was this separate and apart from al-Qaeda?” asked Rep. Adam B. Schiff (D-Calif.), a member of the House intelligence committee. “Any of those are possible,” Schiff said, but accounts of the attack and the firepower employed “indicate something more than a spontaneous protest.”

Others seemed more certain. Rep. Mike Rogers (R-Mich.), chairman of the intelligence committee, said the assault appeared to be well planned and well organized, with attackers executing military-like maneuvers.

A senior U.S. intelligence official said, “We haven’t seen any significant indication of al-Qaeda involvement in this attack,” adding that there are conflicting indications of the extent to which it was planned. “We’ve seen some indications that point us in that direction and others that do not,” said the official, who spoke on the condition of anonymity because of the sensitivity of the situation. He declined to elaborate, except to say that U.S. spy agencies had seen no intelligence indicating such an attack was coming.

U.S. officials said the CIA, the FBI and other agencies were mobilizing to identify and pursue the attackers, an effort that could be aided by U.S. drones that have continued to conduct surveillance flights over the country since Tripoli fell 13 months ago.

Officials said the assault may have been carried out by an affiliate of al-Qaeda, perhaps seeking to avenge the death of a Libyan who had served as the terrorist group’s No. 2 operative until he was killed in Pakistan in June by a U.S. drone strike.

In a somber appearance in the White House Rose Garden, President Obama issued an unusually emotional warning, saying, “Make no mistake, justice will be done.”

But beyond the statements of resolve, there were also expressions of incredulity: How could an American facility that had been attacked just three months ago have remained so vulnerable? How could a city that the United States helped protect during the Libyan uprising have become the setting for such a grisly strike against the United States?

“How could this happen in a country we helped liberate, in a city we helped save from destruction?” asked Secretary of State Hillary Rodham Clinton. “This question reflects just how complicated and, at times, how confounding the world can be.”

The consulate is a walled-off villa with a swimming pool and palm trees on an unpaved side street in the Fwayhat district of Benghazi. It fit in with the other residences in the upscale neighborhood, where walled compounds surround green gardens and dusty streets are lined with shops and cafes.

Benghazi residents said the consulate never had a major security presence, and lawmakers in Washington described it as a “temporary” facility that did not meet the latest State Department security requirements and was scheduled to be replaced. U.S. military officials said it was not guarded by U.S. Marines, as are most embassies and many consulates.

Smith reportedly sent an electronic message to an online video-game counterpart earlier in the evening: “assuming we don’t die tonight,” said the message, according to the counterpart, who posted it online. “We saw one of our ‘police’ that guard the compound taking pictures.”

Stevens, who had spent many years in Libya, had arrived Monday from the embassy in Tripoli for a week of routine meetings. A friend who spent Monday and Tuesday with him said Stevens held meetings with nongovernmental organizations and militia leaders on both days. When the friend dropped Stevens off at the consulate Tuesday afternoon, he said, nothing appeared to be amiss — beyond the protesters.

The first protesters had showed up around noon. Wanis al-Sharif, the deputy Libyan interior minister, said in an interview that the demonstrators were angered by a low-budget American film that portrayed the prophet Muhammad in a blasphemous manner. As the day wore on, Sharif said, the anger escalated and people with weapons infiltrated the crowd.

Preliminary reports speculated that the violence grew spontaneously out of anger over the film. But U.S. and Libyan officials cast doubt on that theory, with some suggesting that the attackers took advantage of the diversion created by protesters.

Sharif said the Libyan government suspected the gunmen were loyal to former leader Moammar Gaddafi, who was overthrown with American help last year and was later killed.

An attack on Sept. 11

Other fingers pointed to possible al-Qaeda affiliates. The suspicion of al-Qaeda involvement was supported by the Sept. 11 timing as well as the release of a recording this week by al-Qaeda leader Ayman al-Zawahiri urging Libyans to avenge the death by CIA drone strike of his deputy, Abu Yahya al-Libi.

There had been signs of a threat earlier. On June 5, a bomb exploded outside the gates of the consulate in the first attack on an American facility since the fall of Gaddafi. No one was injured.

A jihadist group calling itself “Brigades of Imprisoned Sheik Omar Abdel-Rahman” claimed responsibility, according to the Site monitoring service. The group posted a message on jihadist forums saying the attack was a response to the drone strike that killed Libi in Pakistan on June 4. The group is named after the blind Egyptian sheik who is serving a life sentence for his role in the 1993 World Trade Center bombing.

On June 10, two British bodyguards were injured in an attack in Benghazi on a convoy carrying the British ambassador. The assailants used rocket-propelled grenades to attack the convoy as it was pulling out of the British Consulate.

Militants also have been blamed for attacks on the Tunisian Consulate in Benghazi and on the International Committee of the Red Cross in Libya.

A senior Obama administration official, speaking on the condition of anonymity in a conference call with reporters Wednesday afternoon, said there was a “robust American security presence” at the consulate. When asked whether security had been strengthened after the recent attacks, the official said, “We don’t ever talk about the details of those kinds of things.”

The most detail about the events that unfolded Tuesday night at the consulate came from the conference call and from interviews with U.S. intelligence officials along with witnesses, most of whom spoke on the condition of anonymity.

According to these officials, the protest turned into a gun battle around 10 p.m., roughly half an hour after the convoy of militants arrived at the consulate. They said men in the crowd began firing at the consulate. Within 15 minutes, the assailants had broken through the gates, scaled the walls and set fire to the main building. Images captured at the scene showed structures and vehicles engulfed in flames.

Stevens’s friend said in an interview that he returned to the compound when he heard about the fighting and found a chaotic scene. “People were panicking and crying because they were shooting, and with the shooting and explosions, it was really chaos,” said the friend, who insisted on anonymity for fear of jeopardizing his safety.

Another witness, Ben Eissa el-Mahjoub, a member of a media team that was at the scene, said the incident was not a protest.

“Armed groups broke into the American consulate,” he said, with the consulate resisting from inside. He said the security teams had tried to protect the ambassador and the consulate. At one point, he said, someone fired a rocket-propelled grenade from the back of the crowd toward the compound.

A two-hour gun battle

While U.S. security personnel and Libyan guards returned fire, the embassy’s top security officer, who had accompanied the ambassador from Tripoli, tried to get Stevens and Smith to safety inside the consulate. In the smoke and chaos, they became separated. When the security officer made it outside, he could not find Stevens and Smith.

He summoned help and went back inside the burning building. The rescuers discovered Smith’s body, but the ambassador was nowhere to be found.

Under continuing fire, U.S. personnel and other consulate employees fled to an annex next door to the compound. That building, which has its own perimeter walls, came under fire that lasted two hours. Two more Americans were killed in the gun battle; they have not been identified, pending notification of their families.

Around 2 a.m. Wednesday, Libyan security forces and their U.S. counterparts were finally able to secure the compound and resume the search for the ambassador.

But at some point, Stevens had been taken from the consulate to Benghazi Medical Center. U.S. officials said it is unclear who took him or whether he was alive at the time. Photographs posted by Getty Images showed a figure identified as Stevens, his skin smudged with smoke residue, being dragged out by Libyans attempting a desperate rescue.

A senior official at the hospital said Stevens was dead when he arrived. The official, who spoke on the condition of anonymity because he was not authorized to discuss the situation publicly, said that “there were no signs of external injuries,” leading doctors to believe that he had died of asphyxiation. The hospital also received 15 Libyans with injuries that included gunshot wounds and fractures, the official said, but it did not treat any other Americans.

“It was rockets that came in that created the fire and smoke,” said Rep. C.A. Dutch Ruppersberger (Md.), the ranking Democrat on the House intelligence committee. Casting doubt on the possibility that the attack was carried out by a disorganized mob, Ruppersberger said: “This rogue group had weapons. And they were able to shoot.”