NAIROBI, Kenya — The black flag of the Islamic State went up over an old, crumbling coastal town in Somalia at dawn on Wednesday, the latest sign of a sharpening duel between Somalia’s top two militant groups.

According to officials and residents, dozens of masked men carrying heavy machine guns, assault rifles and the Islamic State flag marched into Qandala, an ancient trading post on the Gulf of Aden.

There was no resistance, the witnesses said. The Islamic State fighters simply walked down the main road into Qandala, a town known in Somalia for its old white castle on the sea. Residents stood back, staring with a mix of awe and fear. Even the fishermen who usually ply the warm waters of the gulf did not go to sea on Wednesday, banned by the militants.

“I’m scared that these men will kill everyone — civilians, soldiers, officials,” Jama Mohamed Khuurshe, the town’s commissioner, said at a news conference.