The protracted vote began on Wednesday after 14,000 elders and prominent regional figures chose 275 members of parliament and 54 senators, who in turn chose whether to back President Mohamud for a second term or one of 21 rivals.
Incumbent Hassan Sheikh Mohamud conceded just when the process was about to enter a final round.
Supporters of Somalia's new President Mohamed Abdullahi Mohamed, a veteran diplomat and former prime minister, hope he can be the answer to corruption and extremism in the world's most notorious failed state. Celebratory gunfire rang out in Mogadishu from soldiers supporting Farmajo as he took the lead in the second round of voting, police said.
Farmajo a diplomat and professor was Prime Minister of Somalia from 2010 to 2011.
Ahead of the vote, candidates allegedly paid lawmakers millions of dollars in cash and gifts in an effort to win support.
Seth Meyers ridicules Trump for his 'bizarre' Black History Month comment
I am very proud now that we have a museum on the National Mall where people can learn about Reverend King, so many other things. He did not mention the Martin Luther King Jr. "But that's the way the press is, very unfortunate".
Correspondents said most schools and offices remained open but people had had to walk to reach their destination. "This victory belongs to the Somali people, and this is the beginning of the era of the unity, the democracy of Somalia and the beginning of the fight against corruption". He was referring to the al-Shabab terrorist group. The 55-year-old politician is a father of four and holds both U.S. and Somali citizenship. He also worked at the Somali embassy in Washington.
Somalia has not had an effective central government since the fall of the Siad Barre regime in 1991.
Al Shabaab militants in Somalia publicly beheaded four men accused of spying for the country's Western-backed government, the United States and neighbouring Kenya, residents in the south of the Horn of Africa country said.
The activist organization Transparency International rated Somalia the most corrupt country on earth.
The new president of Somalia is a guy in Buffalo. A joint statement by the United Nations, U.S., European Union and others cited a number of "egregious cases of abuse of the electoral process, including seats reserved for women candidates only that were ultimately taken by male candidates".
The country has not held a democratic popular vote since 1969, and has cycled through dictatorships, coups and clan conflicts ever since. Human rights groups have warned that Somalia is hardly equipped to support the returnees - especially as the United Nations and others warn that drought is creating a humanitarian crisis for nearly 3 million Somalis.