The attackers entered the Ambassador Hotel on Maka al-Mukarama street after setting off a car bomb at the gates outside, witnesses said.
Somalia's state news agency said two MPs were killed in the blast, with another four rescued.
The al-Shabab militant group was quick to say it was behind the attack.
The BBC's Somali Service reporter Ibrahim Aden in Mogadishu says the explosion is one of the largest to hit the city and the scale of destruction is huge.
Somalia's state news agency Sonna said at least 50 people were hurt and that the death toll was likely to rise.
It named the dead MPs as Abdullahi Jama Kabaweyne and Mohamoud Mohamed Gure.
The head of the city's Amiin ambulance service has told the BBC the main Medina hospital is overcrowded.
"I was the first person to reach scene. The damage was enormous," Dr Abdulkadir Andirahman Haji Aden said.
"My brother and his son were among those injured. We are now in Medina hospital and it is overcrowded with wounded people. I have never seen something like this."
Al-Shabab, which is battling the UN-backed government, was driven from Mogadishu in 2011 but still remains a potent threat and frequently carries out attacks in the city.
The government, with the help of African Union forces, is fighting the al-Qaeda-linked group to regain control of the country.
Earlier on Wednesday, the government announced it had killed Mohamed Kuno, the al-Shabab militant who led the attack on Garissa University in Kenya in April 2015 that killed 148 people.
He was one of 16 al-Shabab militants - four of them senior leaders - killed in an overnight raid on their convoy in Kismayo, a port city in southern Somalia.

Lord Hall is reported to have received a threatening email after dropping presenter Jeremy Clarkson from Top Gear because he had punched a producer.
The Mail on Sunday also reported Lord Hall and his wife were under 24-hour protection from security guards at their Oxfordshire home.
The Met said the email appeared to have been sent from outside the UK.
"Police in Westminster are investigating an allegation of threats to kill," a Met spokesman said.
"The allegation was reported to police on Wednesday, 25 March.  The threat was made by email."
He said inquiries were continuing to establish from where the email was sent.
No arrests have been made.
A BBC spokeswoman said: "We wouldn't comment on security matters."
Lord Hall announced his decision not to renew Clarkson's contract on Wednesday - after an internal inquiry found the presenter had subjected producer Oisin Tymon to an "unprovoked physical and verbal attack" at a North Yorkshire hotel.
Mr Tymon, who suffered a split lip, did not file a formal complaint and it is understood Clarkson reported himself to BBC bosses following the incident, and was suspended.
The row - which took place on 4 March - was said to have occurred because no hot food was provided following a day's filming.
Lord Hall said he had not taken his decision lightly, but insisted "a line has been crossed".
More than a million people signed an online petition to reinstate Clarkson.
The Mail claimed the death threats were made to Lord Hall via email the same day as his announcement.
It also published photographs of security guards outside his home.
Mr Tymon, who previously described Clarkson as a "unique talent", has himself come under sustained abuse on social media for his involvement in the dispute.
Speaking outside his home on Thursday, Clarkson asked fans to show restraint.
Gunmen have stormed a hotel in the centre of the Somali capital Mogadishu, with reports of at least 10 dead and 50 wounded.
[NEXT_CONCEPT]
Alleged death threats made against BBC director general Tony Hall are being investigated by the Met Police.