ISTANBUL — Syrian doctors and rescue workers said Sunday that dozens of people had died in an apparent chemical attack on a besieged enclave near Damascus as government forces escalated their offensive to recapture the last rebel strongholds near the capital.

The attack, which killed at least 40 people in the city of Douma on Saturday night, many of them choking and foaming at the mouth, appeared to force the start of a final withdrawal of hard-line rebels from the most strategically important district to remain under opposition control. An agreement allowing them to pull out was announced by the Russian military command in Syria.

More than 500 people “were brought to local medical centers with symptoms indicative of exposure to a chemical agent,” according to the Syrian American Medical Society, a Washington-based nonprofit group that supports health facilities in the area. Footage from the area showed bodies strewn across the floor of an air raid shelter. Among them was a young man who appeared to have died foaming at the mouth and clutching his child.

Rescue workers at the scene said the smell of chlorine in the room had been almost overpowering.

President Trump responded to the attack on his Twitter account Sunday, describing it as an “atrocity.”

“Many dead, including women and children, in mindless CHEMICAL attack in Syria,” he said. “Area of atrocity is in lockdown and encircled by Syrian Army, making it completely inaccessible to outside world. President Putin, Russia and Iran are responsible for backing Animal Assad. Big price . . .”

“Animal Assad” was an insult directed at Syrian President Bashar al-Assad.

The attack came as Syrian government forces stepped up an eight-week-long offensive against Douma, outside Damascus, the last stronghold controlled by hard-line rebels from the Jaish al-Islam group.

More than six years into Syria’s bitter civil war, Assad’s military has repeatedly been accused of using chemical weapons in densely populated areas, a tactic that experts say is intended to sow terror and break morale.

As rescue workers counted the dead Sunday, pro-government outlets reported that a deal had finally been reached for the rebels to leave the area. That was confirmed by Russian Gen. Yuri Yevtushenko, who said that 8,000 fighters would be allowed to pull out, according to the Russian news agency Tass. Jaish al-Islam did not respond to requests for comment.

This image released by the Syrian Civil Defense White Helmets, shows a child receiving oxygen through respirators following an alleged poison gas attack in the rebel-held town of Douma, near Damascus, Syria. (Syrian Civil Defense White Helmets/AP)

Syrian state media and Russia’s Foreign Ministry dismissed claims that Syrian troops were responsible for the deaths in Douma. Russia is a principal ally of Assad.

The allegations are “without basis” and are “designed to shield the terrorists . . . who reject a political settlement,” the Russian Foreign Ministry said. But a network of flight monitors said they had observed a stream of Mi-8 helicopters departing a Syrian government air base and heading toward Douma between 8:25 and 8:45 p.m., shortly before doctors started to record mass casualties.

Escalating U.S. rhetoric against Syrian and Russian tactics in Eastern Ghouta has done little to quell the violence. Late Saturday, the State Department singled out both governments, saying they “must be held accountable.”

Department spokeswoman Heather Nauert described the reports from Eastern Douma as “disturbing” and “horrifying,” saying they required an “immediate response by the international community.”

Gregory D. Koblentz, the director of George Mason University’s Biodefense Graduate Program, said the attack appeared to reflect how much the clout of U.S. policy has faded in Syria.

“Assad is less concerned about Beltway politics, less concerned by who is in the White House. His calculation is based on whether it will help his chances in achieving gains on the ground, or punishing the rebels,” he said.

Multiple reports, including from rescue workers and the State Department, said an initial attack had targeted a hospital. It was unclear, however, what type of chemicals might have been used.

A representative for the United Nations said that Secretary General António Guterres was “particularly alarmed by allegations that chemical weapons have been used against civilian populations in Douma” but that the United Nations was “not in a position to verify these reports.”

Syrian doctors and rescue workers on Sunday shared with journalists graphic images of men, women and children who they said had been killed or wounded in the attack.

“We tried to send people to the area to rescue the injured, but even the rescue workers began suffocating,” said Mohamed Samer, a medical worker in Douma.

Some of the footage showed piles of bodies inside homes or slumped in concrete stairwells, foam visible on their noses and mouths. In other videos, civilians are shown streaming onto a chaotic field clinic where workers were attempting to treat those affected.In one, doctors held an inhaler to the mouth of an infant whose body shuddered through ragged breaths. Another showed an ashen-faced man convulsing.

The images recalled earlier chemical weapons attacks on civilians in Syria, including those involving the nerve agent sarin. A year ago, nearly 100 people were killed in a strike on the northern town of Khan Sheikhoun that the United Nations has blamed on the Syrian air force. In 2013, also in Eastern Ghouta, a sarin attack killed more than 1,000 people — an event that prompted President Barack Obama to threaten military action against the Syrian government.

More than 1,700 people have been killed in Eastern Ghouta, of which Douma is the largest city, since the Syrian army and allied Russian forces began a punishing assault in February to rout rebels from the area.

A U.N. Security Council resolution failed to quell the fighting, and over the past month, more than 130,000 Syrians have left Eastern Ghouta as part of evacuation deals between rebels and government forces, the United Nations said.

As many as 150,000 people remain in Douma, where the humanitarian situation is “severe” and food is “in short supply,” according to the U.N. Office for the Coordination of Humanitarian Affairs.

A fragile cease-fire had shielded Douma’s residents for 10 days as Russia led talks with Jaish al-Islam to negotiate its withdrawal from the area after six years of control. But that respite was shattered Friday when government forces began a fresh round of air and artillery strikes after the rebel group showed little willingness to compromise.

Jaish al-Islam launched volleys of rockets into densely populated Damascus districts as the fighting intensified, killing or maiming residents and striking terror in the broader community.

“I am living the toughest moments of my life,” said one medical worker in Douma, who spoke on the condition of anonymity for security reasons. He said the bombardment has been so intense in recent days that he has been unable to reach his family in Douma.

“Everyone is too scared to go out,” he said.

A victory for government forces in Douma would effectively mark a death blow to the armed rebellion against Assad, whose government has wielded brutal force to snuff out a seven-year-long uprising.

Eastern Ghouta was one of the first areas near Damascus to revolt in 2011. If it falls under full government control, only a handful of areas will remain under rebel control, none of them as strategically important.

Suzan Haidamous and Asma Ajroudi in Beirut, Amie Ferris-Rotman in Moscow and Jenna Johnson in Washington contributed to this report.