NAIROBI, Kenya — Gunfire erupted across the Somali capital, Mogadishu, on Sunday as security forces loyal to the president clashed with units that appeared to have sided with his rivals, stoking fears that Somalia’s simmering political crisis is spilling over into violence.
The fighting, some of the worst in the Somali capital for years, followed months of tense talks between President Mohamed Abdullahi Mohamed and opponents who accuse him of making an unconstitutional power grab.
The talks collapsed after Mr. Mohamed failed to hold presidential and parliamentary elections by February, as scheduled, and then two months later signed a law extending his term in office by two years. His actions have drawn criticism from the United States and other Western allies.
The moves effectively ended United Nations-mediated negotiations backed by the United States and added fuel to an already combustible political situation.
The shooting started Sunday afternoon after soldiers aligned with the opposition took positions at several strategic locations in Mogadishu, drawing fire from pro-government forces. Analysts said the rift was influenced by the powerful clan divisions that have often been at the center of the turmoil Somalia has faced since its central government collapsed in 1991.
As rival factions traded fire late into Sunday evening, alarmed Western officials appealed for a halt to fighting they feared might spiral into a wider confrontation that could unravel years of modest yet steady progress toward turning Somalia into a functioning state.
The European Union ambassador to Somalia, Nicolas Berlanga, appealed on Twitter for “maximum restraint” on all sides. “Violence is unacceptable,” he said. “Those responsible will be held accountable.”
The fighting also raised the possibility of dangerous fissures along clan lines inside the Somali military, and the worry that powerful foreign-trained units, including an elite American-funded commando squad, could get sucked in.
Videos posted online by Somali reporters and news outlets Sunday night depicted long bursts of gunfire around Kilometer 4, a major junction in the city. Some of fighting occurred near Villa Somalia, as the presidential palace is known.
Foreigners living in the highly protected zone around Mogadishu’s international airport said they had retreated into bunkers to avoid being hit by stray gunfire.
The main clashes occurred outside the homes of Hassan Sheikh Mohamud, a former president of Somalia, and Abdirahman Abdishakur Warsame, the leader of a major opposition party. In statements, both men laid blame for the attacks on President Mohamed, who is popularly known by the nickname “Formaajo.”
At a hastily convened news conference, Hassan Hundubey Jimale, Somalia’s minister of internal security, denied that the government had attacked the former president’s home and blamed unspecified foreign countries for the clashes.
Mr. Jimale gave no details about how many people had been killed or injured.
Critics said Mr. Mohamed was making a high-stakes bid to stay in power.
“It seems Formaajo has decided his final suicidal attack by attacking every opposition figure in town,” said Hussein Sheikh Ali, a former national security adviser who once worked under Mr. Mohamed.
American officials said they had privately warned Mr. Mohamed, a one-time American citizen, against using the Danab, an American-trained commando force of about 900 soldiers, to crack down on his opponents. But they acknowledged that Mr. Mohamed has other options, including Turkish-trained troops estimated to number at least 2,600 men.
A contingent of troops trained in Eritrea, whose authoritarian leader, Isaias Afwerki, is a key ally of Mr. Mohamed, are reported to have returned to Somalia in recent weeks.
The election in 2017 of Mr. Mohamed, a former New York State official with a home in Buffalo, raised hopes he could set the country on a less corrupt and dysfunctional track. But disillusionment set in as Mr. Mohamed’s government silenced critics, expelled the top U.N. official and, last year, dragged its feet over scheduled elections.
The opposition has refused to recognize Mr. Mohamed’s authority since his four-year term expired on Feb. 8 without planned presidential and parliamentary elections taking place.
Talks between the two sides over the terms of any elections have been deadlocked since the fall. Opponents accused Mr. Mohamed and his powerful spy chief, Fahad Yasin, of attempting to rig the system by stuffing regional electoral boards with their supporters.
Mr. Mohamed claimed his enemies were trying to shy away from an election, and now says he needs two years to bring forward plans for universal suffrage in Somalia. Under the current system, the president is chosen through an indirect, clan-based vote.
Mr. Mohamed’s move to extend his term by two years on April 14, which some analysts called a “constitutional coup,” met with fierce criticism from the United States and other Western allies.
In Mogadishu, the move caused some opposition leaders to retreat into their clan strongholds.
Among those embroiled in the fighting on Sunday was Sadek John, a former police chief of Mogadishu who was dismissed in mid-April after he opposed Mr. Mohamed, according to a Somali police official who spoke on the condition of anonymity because he was not authorized to talk to the press.
Declan Walsh reported from Nairobi, Kenya and Hussein Mohamed from Mogadishu, Somalia.