The new president is known as Farmajo, Italian for cheese
Somalia's MPs have elected a Somali-US national as the country's new president in a vote held in an aircraft hangar.
Ex-Prime Minister Mohamed Abdullahi "Farmajo" Mohamed beat President Hassan Sheikh Mohamud in a surprise result.
The vote was held at the heavily guarded airport complex in the capital, Mogadishu, as the rest of the country is too dangerous.
Traffic was banned and a no-fly zone imposed over the city to prevent attacks by militant Islamists.
Despite this, suspected militants fired mortar rounds close to the venue on Tuesday night.
Somalia has not had a one-person one-vote democratic election since 1969.
That vote was followed by a coup, dictatorship and conflict involving clan militias and Islamist extremists.
Mr Mohamed's election is part of a lengthy and complex process to help the East African state rebuild its democracy and achieve stability.
More than 20,000 African Union (AU) troops are stationed in Somalia to prevent militant Islamist group al-Shabab from overthrowing the weak government.
The new president is popularly known as "Farmajo", Italian for cheese, because of his love for the dairy product.
Much of Somalia was a former Italian colony.
What has been the reaction to the result?
The aircraft hangar was crowded with MPs
Thousands of Somalis quickly took to the streets to celebrate Mr Mohamed victory and cheering soldiers from the Somali army fired into the air, the Associated Press news agency reports.
Mr Mohamed is seen as a Somali nationalist, and his chances of winning increased after Somalia's arch-rival, Ethiopia, was seen to be backing the defeated president.
Mr Mohamed obtained 184 votes, compared with 97 for the outgoing president, who accepted defeat, avoiding a third and final vote.
"History was made, we have taken this path to democracy, and now I want to congratulate Mohamed Abdullahi Farmajo," Mr Mohamud said in his concession speech.
Did the election go off peacefully?
Yes. The election hall, a converted aircraft hangar packed with MPs, was at the Mogadishu international airport complex.
A 60-second guide to the unique way Somalia is electing its new president
It is viewed as the most secure site in Somalia, as the main AU base is there.
The vote was moved to the airport complex from a police academy because of growing fears that al-Shabab could strike.
The 2012 presidential vote was held at the academy, and the 2007 and 2004 vote in neighbouring Kenya and Djibouti respectively.
Were there only male presidential candidates?
A woman, Fadumo Dayib, had said she would stand but pulled out saying it was marred by corruption. However she has welcomed Mr Mohamed's victory.
More than 20 entered the race on Wednesday, but the number was reduced to two after two rounds of voting.
At least 16 of the original candidates have dual citizenship - nine hold US passports, four UK passports and three Canadian passports, according to a leading Somali private radio station.
It means that if US President Donald Trump's ban on Somali citizens entering the US comes into force again, some of them could be affected.
Many Somalis obtained dual nationality after fleeing the decades-long conflict. The US, UK, Kenya and South Africa are among countries where many Somalis have settled.
How big a threat is al-Shabab?
The militants are suspected to have been behind a series of attacks on the eve of the vote, with two mortar rounds fired close to the voting venue.
Residents in Arbacow village outside Mogadishu say militants also attacked an AU base there.
Al-Shabab has a presence in much of the southern third of the country and has previously attacked the Somali parliament, presidential palace, courts, hotels and the fortified airport zone.
At least 19 politicians, as well as many civilians and soldiers, have been killed in its assaults.
Wednesday's security measures include a ban on flights to and from Mogadishu airport.
Who are al-Shabab?
What is the new president's main challenge?
More than 250 MPs are taking part in the secret ballot
Apart from achieving stability in a country that has not had an functioning government since the fall of the Siad Barre regime in 1991, he has to tackle corruption.
Unconfirmed reports said votes were being sold for up to $30,000 (£24,000) in a country heavily funded by foreign donors, and where most people are poor.
"This is probably the most expensive election, per vote, in history,'' Mogadishu-based anti-corruption group Marqaati said on Tuesday, the Associated Press news agency reports.
Ahead of the vote, the United Nations' envoy to Somalia, Michael Keating, told the BBC the new president would have to tackle corruption.
"It sets the stage for the next president to do something about it. In fact the credibility of the next president will revolve around whether he takes decisive action," he said.
Analysts say holding the election at the airport complex is also aimed at reducing the possibility of vote buying or other corruption during the election process.
Will the UN and AU back the new president?
Yes. The UN and AU see the vote as a building block in efforts to create a stable democracy in the hope that the next president will be chosen in a one-person one-vote election.
The security forces have taken up positions in the largely deserted city to prevent attacks
They cannot ignore Somalia. It is strategically important for international trade, as it lies along the Red Sea and the Indian Ocean.
Foreign navies, including those of the US and China, have a strong presence in the region. They have succeeded in reducing piracy, which was a very big problem until a few years ago.
The US also has a huge military base in neighbouring Djibouti, using it to carry out air strikes on militants in Somalia.
Some analysts also fear that the conflict across the sea in Yemen could spill over into Somalia.
There have been reports that some groups are smuggling weapons into Yemen via the Eastern African state, increasing pressure on foreign powers to improve security in the region.