In the aftermath of the gruesome mob violence in Benghazi this week that resulted in the murders of U.S. Ambassador Christopher Stevens and three other American diplomats, together with as many as ten Libyan security guards, as well as attacks on U.S. embassies elsewhere in the Middle East, it is easy to recall the 1993 Black Hawk Down incident in Somalia, in which eighteen American servicemen were killed, their bodies dragged through the streets of Mogadishu by angry mobs.

Democracy does not tend to flourish in the midst of civil wars, nor in fragile states, much less without a strong partner, such as the nations of Europe and Japan enjoyed under the U.S.-sponsored Marshall Plan in the years following the Second World War. In Somalia after Black Hawk Down, there was no state, there was a hydra-headed civil war, and there was no American goodwill—or anything else for that matter, for a very long time. Soon afterward, the United States withdrew its troops from the country, and Somalia was left to its own devices. The country became an ungoverned black hole where armed gangs fought for turf, sea pirates established a sanctuary to launch raids offshore, and Islamist extremists took refuge after launching terrorist attacks. In the intervening years, by and large, the U.S. presence in Somalia has been fleeting and furtive: lighting raids by Special Forces commandos to snatch or kill fugitive extremist suspects; convoys of naval warships escorting seaborne shipments of food to alleviate the country’s periodic famines, and anti-piracy operations in the seas off its lawless shores. For most of the past two decades, Somalia has languished under widespread international opprobrium as the world’s “most failed state.”

Because of all of this bad history, and the seething volatility currently sweeping through much of the Muslim world, it is worthwhile to take note of the fact that on Monday in Mogadishu, a peaceful Presidential transition took place. It was the first time since the nineteen-eighties that a new President was selected in a vote that was conducted inside Somalia. Somalia is not yet a full-fledged democracy, but it appears to be on its way.

Sheikh Sharif Sheikh Ahmed, who became President in 2009, is a former Islamist radical turned moderate who bears the historic merit of having overseen Somalia’s emergence from a charnel ground into a place where a semblance of stability has begun to emerge. Three years ago, on a reporting assignment to Mogadishu for The New Yorker, I spent two weeks as Sheikh Sharif’s guest in the Presidential compound. I witnessed daily exchanges of gunfire and artillery between the Ugandan African Union peacekeeping troops guarding him and the Shabaab insurgents who had free run of much of the rest of the city. The situation was precarious, and there seemed no reasonable assurance that Sharif would live to see the end of his term, or, for that matter, that he would be able to achieve much while in office; his writ extended no further than the walls of his compound.

Things have improved greatly since then. The pace of change quickened considerably beginning last summer, when the Shabaab were pushed out of the capital during a famine that brought an influx of international relief agencies to Mogadishu, greater action by the African Union troops stationed there, and new contingents of soldiers from neighboring Kenya, Djibouti, and Ethiopia. Now, for the first time since the end of the Cold War, reconstruction and development is taking place in Mogadishu—even as fighting continues in the south, where the Shabaab remain active. A few hotels and restaurants and other businesses have opened up; émigrés have returned to invest in their homeland. A few months ago, Turkish Airways inaugurated twice-weekly flights between Istanbul and Mogadishu.

When I was in Mogadishu in 2009, by contrast, I had to be ferried to and from the Presidential compound and the airport in a convoy of armored personnel carriers manned by troops carrying heavy machine guns. Goats grazed on weeds in the downtown wastelands where government buildings had once stood; there were donkey carts, but few vehicles, and almost no people visible on the city’s streets.

During his tenure, President Sharif achieved little more than to survive his term in office, but that in itself was an achievement that brought results. Although neither an inspiring nor flawless leader, Sharif was fair-minded, and bears the historic distinction of helping secure his country’s climb toward democracy, now hopefully inexorable. After surviving numerous attacks, including suicide bombings that killed a grievously large number of his cabinet ministers and friends, not to mention peacekeeping troops and innocent civilians, Sharif stepped down without fuss after his defeat in Monday’s parliamentary vote. He urged his countrymen to support his successor, Hassan Sheikh Mohamud, a professor in his fifties.

Democracy’s enemies are never very far away, and in Somalia, they lie in wait like hyenas waiting to strike at a passing herd. On Wednesday, barely forty-eight hours after he was sworn into office, three suicide bombers struck the secure compound where Mohamud was delivering a press conference, together with the visiting Kenyan foreign minister. Five people died in the attack, including the bombers, who were gunned down by African Union troops. President Mohamud and his V.I.P. guest were unharmed, and carried on speaking. With a display of admirable aplomb, given the circumstances, Mohamud said, “First and foremost, we will address the issue of security in Somalia. It is our number-one priority, our number-two priority, and our number-three priority.”

When I was in Mogadishu, President Sharif’s entourage spoke dreamily of a future day when major foreign nations might one day reopen their embassies in the Somali capital. At the time, it was a daydream, pure and simple. In the past year, countries like Italy and Great Britain and Turkey, and a handful of others, have quietly done so—although most of the ambassadors do not yet live there permanently but come and go, discreetly, from homes in Nairobi. However periodic their presence, the fact that they come at all is a matter of desperate importance to the Somalis, a sign that they belong to the larger world, and that the larger world knows they exist. In the wake of the attacks this week on the U.S. missions in Benghazi, Cairo, and Sanaa, there is understandably heightened concern about the risks faced by U.S. diplomatic representatives who are stationed abroad. There will be calls for greater security measures to protect them, and, no doubt, greater restrictions on their movements as well.

The reopening of a U.S. embassy in Mogadishu may well now be seen as an unnecessary risk, but it would be a great pity if that were so. The American disappearance from Somalia all those years ago did not help Somalia; nor did it help the United States. Now that Somalia has begun moving toward democracy, it certainly deserves American support. If the U.S. democratic ideal is still to be emulated elsewhere, the U.S. must be seen to be brave about its purpose, despite the risks, and above all, it must be visible. Now more than ever, and precisely because of what has happened in Benghazi this week, the United States should promptly and forthrightly restore its diplomatic presence there, and, for good measure, reopen its embassy in Mogadishu as well.

Photograph of Hassan Sheikh Mohamud, by Abdurashid Abikar/AFP/Getty Images.