MOGADISHU, Somalia (AP) _ The last tape of the American anthem had already shipped out. So Marine Lance Cpl. David Ellinghausen sang ''The Star-Spangled Banner'' live as the Mogadishu operation was handed over to Pakistan.

Monday's ceremony came days before the United Nations takes full control of the country from Washington.

The U.S.-led coalition came ashore Dec. 9 to help in the distribution of aid to the starving and sick caught in the country's civil war. Some 350,000 people died last year in Somalia from famine and disease.

Next week - possibly Tuesday - the United Nations is expected to assume overall operational control, and start the mammoth task of rebuilding war-torn Somalia. More than 4,000 Pakistani troops are in Somalia.

Monday's ceremony had a bit of an impromptu flavor, after it was discovered that last known tape of the American anthem in Somalia had apparently been packed in a hurry by other Marines who'd already flown home.

So the 22-year-old Ellinghausen, who had sung the anthem at a couple of basketball games back home in Mount Carmel, Ill., agreed to perform.

Earlier, a group of 24 Somali students in red Marine T-shirts performed traditional songs.

They are among the 1,600 kids who attend the Botello primary school, a battered gymnasium that has been renovated with Marine help and named after Lance Corp. Anthony Botello, who died in the line of duty here.

Marine Col. Buck Bedard, who has been commanding U.S. forces in the capital, also spoke to the soldiers, Somali police officers and civilians gathered at the ceremony.

He said he took a final drive around the city to compare conditions with when he arrived.

''It was a city where the gun ruled. No one went out at night,'' Bedard recalled. ''Today, schools are open. The Somali police force is back in their barracks. Shops are open. Markets are thriving. We have freedom of movement, day or night. I feel very good about what we have done.''

Also Monday, a U.S. amphibious unit containing 3,500 Marines and sailors pulled out from the southern port city of Kismayu, where it has been bolstering Belgian forces. Their departure reduces the American contingent in Somalia to 7,000.

U.S. Army soldiers hand over Merca, the last sector in the Americans' control, to the troops from Pakistan on Wednesday. The U.N. begins shouldering most of the operation's costs on Saturday.

Marines and soldiers have been leaving in groups of 350 or more nearly every other day for the last week. Americans, as part of the U.N. force, will number about 4,000 by next year.

The remaining U.S. troops' duties will vary widely. They will include a quick-reaction force that will respond to outbreaks of trouble in the country.

Although some American personnel will remain in Mogadishu - both on the streets and in command and support roles - they are largely being replaced by the Pakistanis.