BEIRUT, Lebanon — The Syrian Army soldier had long served in Palmyra, but he was on leave when he heard that Islamic State militants had attacked a village northeast of the desert city, killing dozens of his comrades. He sent frantic text messages, trying to reach them. No one answered.

He shared his anguish last week in a series of texts as he slowly pieced together bits of the story from survivors of the massacre. Soldiers told him they had run out of ammunition. One officer radioed to headquarters, “We’re finished.” Worst of all, the soldier said, was the photograph he was shown of the decapitated body of a friend, the 19-year-old daughter of a Syrian general.

Within a matter of days this week, the Islamic State, also called ISIS or ISIL, seized with apparent ease the cities of Ramadi in Iraq and Palmyra in Syria, in both cases seemingly coming out of nowhere to rout government forces. On Thursday, the militants were digging in, consolidating their grip and executing people with ties to the old order.