Somalia has started to train forces to tackle the pirates
Somali pirates have boarded a ship heading for Mogadishu harbour and shot dead its Syrian captain, officials say.
The captain had refused the pirates' demand to turn the ship away from the port, officials added.
This is believed to be the first such attack so close to the Somali capital. Pirates normally launch their raids further north, especially around Eyl.
African Union peacekeepers and Somali security forces intervened and rescued the Panama-flagged ship.
The AU mission is in charge of security at the Mogadishu port.
The BBC's Mohammed Olad Hassan in the city says the attack has led to security concerns among port officials.
Three crew members were reportedly also injured in the violence.
Somalia's Minister for Ports Abdiasis Hassan told the Reuters news agency that normally police are sent out to escort commercial ships into port.
But he said this time the pirates were already on board and opened fire, injuring one policeman.
The port is used to bring in food aid and military supplies to the Somali capital.
In the absence of any effective national government since 1991, pirate gangs have set up in parts of Somalia, especially the semi-autonomous region of Puntland.
International navies have been deployed to protect ships in the Gulf of Aden, one of the world's busiest shipping lanes.
The AU peacekeepers are helping to protect the UN-backed government, which only controls parts of Mogadishu.
Much of the rest of south and central Somalia is in the hands of hardline Islamist groups.
After 18 years of anarchy and conflict, some three million people - about half the population - need food aid, donors say.