At least 51 civilians were killed, all or most of them shot, and more than 300 wounded, doctors and health officials said. Security officials said at least one police officer died as well.

The mass shooting was the deadliest single episode of violence since the 2011 revolution that toppled Hosni Mubarak, Egypt's longtime autocratic leader. It immediately escalated the nearly week-old confrontation between the generals who forced out Mr. Morsi, Egypt's first democratically elected president, and Mr. Morsi's Islamist supporters in the streets.

In an early sign that the mass shooting had undercut important support for the military's ouster of Mr. Morsi, the country's top Muslim cleric, Grand Imam Ahmed al-Tayyeb, threatened to go into seclusion until the violence ended. The grand imam, who participated in talks on a post-Morsi transitional government, said in a statement broadcast on Egyptian state television: "I might be forced to enter into a retreat in my home until everyone takes responsibility for protecting the sanctity of blood and preventing the country from a civil war."

The military said its soldiers had fired in response to an attack by gunmen from a "terrorist group" who had attempted to storm the facility, according to Ahram Online, the Web site of Egypt's leading newspaper.

Dozens of Islamists who had gathered to hold a vigil for Mr. Morsi denied there was any provocation for the attack. Two bystanders who had supported Mr. Morsi's ouster said that the demonstrators were unarmed and ran in terror as the attack began.

Bullet holes in cars, lampposts and corrugated metal barriers indicated that gunfire was coming from the top of a nearby building where the sandbag barriers around makeshift gun emplacements were visible. Bullet casings on the ground and collected by Islamist demonstrators bore the stamp of the Egyptian Army.

But Egyptian state television showed film of a pro-Morsi protester firing what appeared to be a homemade handgun at advancing soldiers from behind a corner about 250 yards away. The footage was in daylight, hours after the initial shooting began.

A witness who lived nearby said he saw two men with similar weapons among the protesters.

Another clip broadcast on state television, also in daylight and so hours after the attack had begun, showed a masked man among the pro-Morsi demonstrators.

The protesters, witnesses and video footage all appeared to portray the pro-Morsi demonstrators as attempting to fight back against the soldiers by throwing rocks.

Early in the morning, Egyptian state media sent out a news alert saying that an army lieutenant had been killed and 200 "armed individuals" were captured, then hours later reported that there were also dozens of civilian casualties.

There were pools of blood on the pavement. Some of the blood and bullet holes were hundreds of yards from the walls of the facility's guard house, suggesting that the soldiers continued firing as the demonstrators fled.

Ibrahim el-Sheikh, a neighbor and a brother of a Cairo-based New York Times employee, said the police officer, Mohamed el-Mesairy, was killed by military fire. The officer was hiding in a car in the parking lot of a building in a side street that the Morsi supporters were using for shelter. Video footage taken from a window above showed gunfire from the advancing soldiers hitting the car.

Mr. Sheikh, who signed a petition and joined protests for Mr. Morsi's ouster, said he and others carried the officer's body out of his car. "He did not have a head any more," he said.

The Nasr City hospital, a few minutes' drive from the scene of the shooting, began receiving hundreds of victims around 4 a.m. and at least 40 were dead, according to Bassem al-Sayed, a surgeon. The doctor said all the victims he saw were men with gunshot wounds.

The emergency wards and the intensive care unit were full of patients and distraught relatives. Near the emergency room, two dozen men lined up to donate blood.

Dr. Sayed said he had seen similar scenes in the hospital only once: around January 25, 2011, when Egyptians began their revolt against President Hosni Mubarak.

"This is worse," he said.

Mayy El Sheikh and Asmaa Al Zohairy contributed reporting.