The massive attack that rocked a bustling Mogadishu street Saturday, killing 300, was a demonstration of resilience by ­al-Shabab, an Islamist militant group that has been battling U.S.-backed forces.

The group frequently targets public spaces in Somalia’s capital with the goals of undermining the central government’s legitimacy and pushing back against its cooperation with the United States and a coalition of African allies.

While U.S. officials and other observers say Saturday’s bombing does not necessarily signal ­al-Shabab’s resurgence, it does highlight the group’s ability to absorb setbacks and survive to execute brazen attacks.

The militant group has experienced territorial, financial and recruitment losses since 2010, when the United States and its coalition began targeting its command structure. But that has not prevented the group from massing to attack military bases or coordinate bombings inside and outside Somalia, said Seth G. Jones, director of defense policy at the Rand Corp., a Pentagon-funded think tank in Washington.

“Al-Shabab has areas where they can operate relatively freely,” Jones told The Washington Post, including strongholds in southern river valleys. It’s challenging terrain for ground troops to navigate, and the dense foliage can obscure drone surveillance.

Somali security forces and others gather on Sunday to search for remains at the scene of a deadly attack in Mogadishu, Somalia. (Farah Abdi Warsameh/AP)

Attacks against Somali troops and those assigned to the African Union Mission in Somalia (AMISOM), a force numbering about 22,000, have grown more spectacular as al-Shabab’s territory has shrunk, Jones concluded in a 2016 report on the counterterrorism campaign there.

In June 2016, for instance, 30 Ethio­pian soldiers were killed — along with 150 al-Shabab militants — in an attempt to overrun a base some 200 miles north of Mogadishu.

The group has been dangerous to U.S. troops, too. Navy SEAL Kyle Milliken, a senior chief petty officer, was killed in May during a mission described as "behind" Somali troops but later revealed as a mission shoulder-to-shoulder with them.

As U.S. airstrikes have targeted al-Shabab’s commanders and its training camps, the group’s leaders have made security adjustments, Jones said. They rely less on computers and cellphones, whose transmissions can be tracked.

The U.S. military presence in Somalia is about 400 troops, with approximately half in Mogadishu, the Pentagon said Monday. Those forces include Special Operations troops to train Somali forces and conventional personnel on hand for logistical support, although much of the focus has been on collecting and handing off surveillance and intelligence data to the Somalis.

Al-Shabab still controls many rural areas in the south, including the roads leading to cities and towns. Even in Mogadishu, militants go around collecting “taxes” from businesses to offset revenue losses of seaside ports recaptured by government forces.

AMISOM troops heading to Baidoa, one of the largest cities in Somalia, must often use aircraft to get there rather than roads primed for ambushes, said Abdi Ismail Samatar, a Somalia analyst and geography professor at the University of Minnesota who also serves on the board of the University of Mogadishu.

“They are fighting with a bunch of guerrilla fighters who are exceptionally mobile, who are working in a huge territory in which there is no government, so they are able to do what they want at a time of their own choosing,” he said.

A State Department official acknowledged that Saturday’s truck bomb shows that al-Shabab is capable of staging attacks throughout the country, but said the government is making progress in helping Somalia transition from rule by warlords and clans to a federal government with a countrywide reach.

“The magnitude of this attack caught everybody by surprise,” said the official, speaking on the condition of anonymity under State Department ground rules for briefing reporters.

“As terrible as the individual attack is, the fact is we don’t see this every day now in Somalia. This is extraordinary, and it doesn’t change the overall positive trajectory we’re seeing there.”

The State Department official said the Somalis have thwarted a number of planned attacks, and defections have been encouraged by the newly elected president, the Somali American Mohamed Abdullahi Mohamed. He noted that the latest bombing was condemned by Sheikh Mukhtar Robow, a former al-Shabab leader who defected after the Trump administration removed him from the most-wanted list of terrorists run by the State Department’s “Rewards for Justice” program.

“I wouldn’t characterize the overall situation as the central government is failing or returning to a failed state,” the official said. “Under the previous and current governments, we’ve seen some pretty significant improvements.”

The United States over the past six years has spent $1.5 billion on humanitarian aid for victims of drought and famine, and $240 million on economic development aid, including good-governance projects and new schools and government buildings.

The United States also has a diplomatic presence of sorts, although it is limited to the heavily fortified Mogadishu airport, where Somali officials must travel to visit. The embassy officially is in Nairobi, a reflection of the dangerous environment, although it has been rotating diplomats on temporary duty between Somalia and Kenya until it can establish a full-time embassy on the airport grounds.