A former president who ruled from 2012 to 2017, Mr Mohamud defeated incumbent leader Mohamed Abdullahi Mohamed by 214 votes to 110 in a third-round run-off.
In a victory speech to the nation after being sworn in, Mr Mohamud struck a conciliatory tone, promising to "heal any grievances" as the country moves forward.
The Horn of Africa country was supposed to choose a new leader in February 2021 but missed the deadline as Somalia's political leaders squabbled over the election process.
The impasse turned violent when Mr Mohamed, better known by his nickname Farmajo, extended his term in what opponents decried as an unconstitutional power grab.
Mr Mohamed appointed his prime minister to arrange a fresh vote but the task drove a wedge between the two men, putting the vote even further out of reach.
The crisis paralysed the government at a time when stability was badly needed. The new president will need to repair the damage caused by months of political chaos and infighting, both at the executive level and between the central government and state authorities.
The former education campaigner and peace activist also faces other key daunting tasks in the nation of 15 million.
He has vowed to transform the troubled Horn of Africa nation into "a peaceful country that is at peace with the world."
Who is Hassan Sheikh Mohamud?
Born in 1955 in Jalalaqsi in the central Hiran region, Mr Mohamud studied at Somalia's national university before civil war broke out in 1991 and then at Bhopal University in India.
He worked with the UN children's agency, Unicef, before co-founding the Somali Institute of Management and Administrative Development in 1999.
Before joining politics, he spent two decades working in education and in conflict resolution. The latter field is likely to prove a useful skill in his second term.
A member of the powerful Hawiye clan, Mr Mohamud entered politics in 2011 when he founded the Union for Peace and Development Party.
Few expected the bookish activist to become president and his 2012 election victory raised hopes that the fragile nation was on the path to stability.
His government was the first to be given global recognition and billions in foreign aid since the collapse of Siad Barre's authoritarian regime in 1991.
He has links to Al Islah, the Somali branch of the Muslim Brotherhood. The group claims to reject violence in pursuit of an Islamic state, the Counter Extremism Project said.
But the technocrat's term was marked by corruption scandals and political turmoil.
What threats does he face?
Mr Mohamud confronts a familiar threat that dogged his government and others — a deadly and persistent insurgency by the Al Shabab extremist group.
In March, the UN renewed the mandate of a 20,000-strong African Union force, formerly known as Amisom, that has been on the ground since 2007 to support the foreign-backed government in confronting the Al Qaeda-linked extremists.
The reconfigured mission, now known as the African Union Transition Mission in Somalia, or Atmis, is aiming for a more offensive strategy than in recent years with the goal of gradually reducing troop numbers to zero by the end of 2024.
On Monday, US President Joe Biden ordered the re-establishment of fewer than 500 US troop presence in Somalia to help local authorities combat the Al Shabab militant group, a senior American official told reporters.
The move reverses an order from Mr Biden's predecessor, Donald Trump, who in late 2020 pulled nearly all US forces from the East African nation as he sought to wind down US military engagements abroad during his final weeks in office.
Congratulating the newly elected president, US Secretary of State Antony Blinken urged him to develop "security forces to prevent and counter terrorism and assume full security responsibility from the African Union Transition Mission in Somalia."
Somalia's international backers have given warnings that the prolonged infighting over the election has distracted from the threat of Al Shabab, which has consolidated its rural territory and in recent months stepped up attacks.
What risks threaten the people of Somalia?
Somalia is vulnerable to climate shocks and currently suffering its worst drought in decades.
The UN says about 6.1 million people — roughly 40 per cent of the population — have been affected and 760,000 people have fled their homes.
Humanitarian organisations have given warnings that without a significant increase in aid, Somalia could soon face the kind of famine not seen since 2011 when 260,000 people perished from hunger.
The government has little capacity to tackle the problem on its own.
But observers say political stability in Mogadishu would help in co-ordinating the emergency response, and presenting a unified appeal for aid.
A poor, indebted country lacking critical infrastructure, Somalia is dependent on foreign aid to function.
The World Bank estimates nearly three-quarters of the country's 15 million people live on less than $1.90 per day.
The election delays threatened a crucial assistance package from the International Monetary Fund, which expires automatically on Tuesday if a new administration does not approve key reforms.
The government has asked for a three-month extension to this deadline.
The economy grew by 2.9 per cent in 2019 and was tipped to expand further in 2020.
But it instead contracted as the coronavirus pandemic, a locust infestation and floods took their toll, the World Bank said, and forecasts are still below pre-Covid projections.
Pervasive corruption also continues to be a problem ― Somalia sits near the bottom of Transparency International's world corruption index, ranking 178 out of 180 nations alongside Syria.
AFP and Reuters contributed to this article.