Incumbent President Hassan Sheikh Mohamud, who took office in 2012, withdrew from the final ballot after losing 184 to 97 to Farmajo in the second round.
“History was made, we have taken this path to democracy, and now I want to congratulate Mohamed Abdullahi Farmajo,” Sheikh Mohamud said.
Following the announcement, cheering crowds thronged the streets of the capital as well as in the Somali-dominated neighborhood of Eastleigh in the Kenyan capital, Nairobi.
After taking the oath of office, Farmajo said his win represented the interests of the Somali people.
“This is the beginning of unity for the Somali nation, the beginning of the fight against Shabaab and corruption”, a triumphant Farmajo said.
The 55-year-old father of four, better known as Farmajo, holds both American and Somali citizenship.
As president, Farmajo inherits a country where al-Qaeda-linked group, al-Shabaab, continues to pose a huge security threat, attacking government offices and private businesses.
He also faces many challenges in governing Somalia, one of the seven Muslim majority countries that President Donald Trump included in his executive order on immigration.