Mohamed Abdullahi Farmajo has been declared the new President of Somalia after winning the race with 184 votes out of a total of 328.
At least 20 candidates were contesting but majority dropped out after the first round, leaving the top three to go for the second round.
The incumbent, Hassan Sheikh Mohamud came second with 97 and former president Sheikh Sharif Sheikh Ahmed came third with 46.
In the first round, the incumbent had emerged the front-runner but they all fell far short of the required two-thirds majority to win outright.
Results from first round of voting, Hassan Sheikh Mohamud had 88, Mohamed Farmajo scored 72, Sheikh Sharif came third with 49, and Omar Abdirashid Sharmarke came last with 37.
Omar Abdirashid Sharmarke later withdrew from the race, leaving the top 3 candidates for the second and last round of voting.
Mr Fermajo, the former prime minister resigned the position in 2011 as a compromise to extent the tenure of then transitional federal government following deadlock between the executive and parliament.
After his resignation, there were reports that President Museveni had forced him to resign. But in an interview Daily Monitor he blamed political infighting.
African Union troops from the Ugandan contingent and the Somali National Army closed most of roads to the Mogadishu Airport where the voting took place under tight security.
African Union Mission to Somalia [AMISOM] tweeted that the measures were meant to secure elections that had 22 presidential candidates. “AMISOM is working closely with the Somali National Security Forces to ensure the security of the election,”
Despite heavy deployment, alShabaab had attempted to attack the airport on Tuesday night by firing mortars that landed near the venue.
Voting was supposed to take place at a place academy in Mogadishu but was moved to which is viewed as the most secure place in the Somali Capital because of growing security concerns.
According to the Somali electoral system, the president is elected by members of parliament who were earlier elected in December 2016 by 14,025 clan representatives.
Somalia has not had one-person-one vote for decades because of insecurity. President Hassan Sheikh Mohamud is standing for re-election.
Voters were not allowed to carry phones to the venue and they had line up for strict checking to avoid breach of security. According to BBC, all flights to and from Mogadishu were cancelled.
Sources say holding elections in the airport would also reduce the possibility of vote buying which marred the process during the campaigns.
Before the voting started, there was a standoff between security personnel and Members of Parliament over strict security guidelines.
At least 320 lawmakers from the Upper and Lower Houses of the Federal Parliament cast their vote through secret ballot.
If none gets the required number in the first round, the top three will advance to the second round, with the third place in that round being eliminated.
Mohamed Osman Jawari, the Speaker of the Parliament voted first. By 9am, according to AMISOM tweets, the registration of MPs has started. Abdirahman Beileh, the chair of the Presidential Election Committee led the exercise.