Shabab militants launched a major suicide attack on Sunday against a military intelligence base in the capital Mogadishu, setting off a car bomb before storming inside, security officials said.
Somalia's interior ministry said the three attackers were all killed in the raid, and that Somali security forces who fought them suffered no casualties.
"There was an attack against a base belonging to the security forces. The violent elements used a car bomb to get inside the compound. There was a heavy exchange of gunfire," said Adan Mohamed, a Somali security official.
Witnesses near the base, belonging to the internationally-backed government's National Intelligence and Security Agency (NISA), reported a loud explosion followed by intense gunfire.
"There was a heavy explosion and in seconds heavy gunfire broke out. We cannot go outside the house because of the shooting," said Abdulahi Yare, a resident who lives near the base.
In a brief statement, NISA said the attack had failed.
"The attack was successfully thwarted by our forces. None of our buildings nor bases were entered," it said.
Somali officials displayed three corpses after the attack, which came at the start of Islam's holy fasting month of Ramadan -- a period when the Al-Qaeda-linked Shabab, who are fighting to topple the Mogadishu government, have in the past intensified attacks.
"The security forces have foiled an attempted attack by the desperate terrorists," interior ministry spokesman Mohamed Yusuf told reporters.
"One of them detonated himself and two others were shot dead. They were trying to storm the premises but they have been taken out before they reached their objective. There was no casualty on our side," he added.
The Shabab, meaning "youth" in Arabic, emerged out of a bitter insurgency against Ethiopia, whose troops entered Somalia in a 2006 US-backed invasion to topple the Islamic Courts Union that was then controlling the capital Mogadishu.
Shabab rebels continue to stage frequent attacks, seeking to counter claims that they are close to defeat due to the loss of territory in the face of an African Union and Somali government offensive, regular US drone strikes against their leaders and defections.
The group have also carried out a string of revenge attacks in neighbouring countries -- including the September 2013 attack on the Westgate shopping mall in the Kenyan capital Nairobi which left at least 67 dead and the April massacre of close to 150 students in Garissa.
Somalia has been wracked by instability since the collapse of Siad Barre's hardline regime in 1991, and the country's new government is being supported by a 22,000-strong AU force that includes troops from Burundi, Djibouti, Ethiopia, Kenya and Uganda.