The enthusiastic squawking from the orchestra pit in the Police Academy theatre in Mogadishu somehow seemed oddly appropriate. The energetic, rhythmic Somali music that entertained the crowd during the presidential inauguration ceremony was off-key, slightly manic, and strangely discordant.

President Hassan Sheikh Mohamoud took the seals of office from his predecessor Sharif Sheikh Ahmed in the ramshackle hall in a ceremony with none of the polish or pomp of even Somalia's poorest neighbours. It was a chaotic affair, in a rundown building chosen not for any symbolic reasons, but simply because it was the only place in Mogadishu big enough to host hundreds of guests and foreign dignitaries, and be protected from insurgent attacks.

The feeble air conditioning barely kept the sweat off the suited crowd the public address system was so distorted the speeches were barely comprehensible, and the honour guard was out of step. But for all its flaws and quirks, the ceremony was a triumph of belief and courage in the face of staggering adversity. It also had a soundtrack that fitted the moment perfectly.

Consider the job ahead of President Mohamoud.

Somalia is routinely described as "the World's most failed state", and a grim example of what anarchy really means. Twenty years of constant war has ground the infrastructure to dust and two million Somalis need food aid just to survive. And if the president – or anyone else – underestimated the security challenge at all, al-Shabab set him straight by trying to kill him less than two days into the job.

The president-elect was hosting a news conference with Kenya's foreign minister Sam Ongeri on the top floor of a hotel within sight of Mogadishu's airport – one of the most heavily guarded places in all of Somalia. The Jazeera Hotel was itself in a heavily patrolled buffer zone considered to be relatively safe. Moments into the conference, just as the foreign minister began his opening remarks, a blast shook the building. Then, a few seconds later, gunfire erupted in the street below.

Security guards hustled dignitaries to the ground, journalists dashed for cover by the windows, desperate to stay out of the line of fire, but trying for a view of the drama below.

I watched as a wounded gunman stumbled into the compound and then fell to the ground. As he tried to rise, a security guard shot him dead. It was just as well. It later emerged that the attacker died before detonating the suicide vest he was wearing beneath his tunic.

Then, just as the commotion faded and both security forces and officials began to recover their composure, another suicide bomber attacked.

Eight people died in the assault, including the three attackers. Mohamoud and his entourage were unharmed, but the incident still made it clear that whatever is happening on the front lines, al-Shabab can still cause havoc.

Between the rebels' constant harassment, Somalia's own self-destructive record of clan conflict and the rolling humanitarian crisis, there was every reason to believe that the country would remain "failed" forever.

The fact that Somalia's own political "road map" ever made it to the inauguration was credit to the combined efforts of the United Nations diplomats and the six Somali "principles". The principles included the outgoing president Sheikh Sharif Sheikh Hassan, Prime Minister Abdiweli Ali Mohammed, former speaker Sharif Hassan and three of the most powerful provincial leaders.

More cynical observers always argued that the process had been manipulated from the outset that somehow money or clan or old-fashioned death threats had corrupted the outcome. Or that powerful figures, with vested interests in the status quo, would conspire to stop the road map from ever reaching the elections. Or that the UN was sticking to its timetable far too rigidly, forcing Somali politicians to move well before they were ready.

All of those criticisms remained, even on the morning the elections were held. It was only when Mohamoud emerged victorious with such an overwhelming margin, that it became clear the UN's gamble had worked.

So although the band at the inauguration ceremony was almost comically out of tune, the fact that they were playing together at all, somehow seemed a fitting metaphor for the new Somali government. It is a long way from sounding like a symphony, but somewhere behind all the noise is a tune.