“Look at you. You’re here, we’ve been protecting you all week — maybe you didn’t even know it,” he said then. “We want peace; we crave it more than you could ever understand, to get out of this darkness, to stop killing each other, to stop being the laughingstock of the world.”

After Ethiopia invaded Somalia in December 2006, the Islamist group that briefly controlled Somalia went underground, and Mr. Robow with it, orchestrating hit-and-run attacks on Ethiopian forces and Somalia’s weak government.

Insurgent fighters banded together under the name Shabab, which was one of the Islamist factions in Somalia at the time. Mr. Robow became a Shabab spokesman and a top field commander; at one time he was thought to be the No. 2 or No. 3 in the organization.

The American government, however, considered Mr. Robow a terrorist and offered a $5 million reward for information leading to his capture. The Trump administration quietly dropped that award, officials said in June, apparently because Mr. Robow had entered into secret talks with Somalia’s government.

Mr. Robow’s defection from the group shows the Shabab may be more divided and at war internally than it has been for years. Still, it remains a regional menace, sending hit squads across Somalia and terrorizing neighboring Kenya.

It’s not clear if Mr. Robow will be given a position in the Somali government or placed under house arrest or possibly even put on trial. He had not been an active militant for several years, though he still commanded a sizable force of loyal young gunmen who were in his clan.

Somali officials indicated on Sunday that they had not decided yet what to do with him.