Muammar Gaddafi's forces have bombarded the port of the besieged city of Misrata while a humanitarian ship was evacuating hundreds of foreign migrants and injured Libyan civilians, killing up to five members of a migrant family.

The Red Star 1, a ferry chartered by the International Organisation for Migration, had been forced to wait outside Misrata for four days after loyalist troops planted sea mines and pounded the harbour with missiles.

The port controller has finally given the ship the go-ahead to dock following a lull in shelling. But as containers of food and medical supplies were being unloaded from the ship, up to a dozen missiles were fired into the area of the port where migrant workers were gathered.

Witnesses said one missile hit a Nigerian or Ghanaian family, killing at least two children and their mother. Others said that the father and a third child had also died.

Julius Chuks, a 35-year-old Nigerian welder, said: "When the attacks started, everyone just started running, though we did not know where to run to. We are very lucky to be alive."

The bombardment caused panic on the ship, with some of the crew keen to leave without boarding the migrant workers or injured people, fearing another missile attack.

Having somehow gained access to the port, dozens of Libyan families desperate to leave Misrata tried to force their way on to the ship, many of them successfully, causing rebel soldiers to fire in the air.

More than 800 migrants, most from sub-Saharan Africa, who have been waiting for weeks to leave Misrata, arrived soon after, packed into trucks. Many of them had been unable to even bring a suitcase along due to the chaos and rush that followed the shelling of their camp.

About 35 injured or critically ill Libyans were wheeled on to the car deck, where makeshift wards had been set up. Shortly after departure, the boat returned to the quay to unload one of the patients, after doctors on board judged would not survive the 20-hour sea voyage. About 100 African migrants were unable to board the ship due to a lack of space.

Finally, in the early afternoon, the Red Star left the port and set sail into the Mediterranean towards Benghazi, the rebel capital in eastern Libya.

"When we came to Misrata before, there was shelling, but this was the closest to the port," said Othman Belbeisi, the head of mission for the IOM, which has evacuated about 5,500 people from Libya's third biggest city since March.

"It was a very dangerous situation, and the captain wanted to leave when the attack started. But we had to take the risk of staying because it was impossible to leave behind the migrants and the medical cases."

Gaddafi, whose forces have been largely defeated in Misrata by the rebels' citizen army, has pledged to shut the port, saying it was being used to ferry in light weapons from Benghazi. But many people in Misrata believe that he is trying to starve them out, as the port is the city's lifeline, allowing in food and other basic goods.

On board the Red Star 1, some of the migrants spoke of their ordeals.

"When the fighting started in Misrata we had to stay indoors all the time," said Samuel Ampansa, a 28-year-old mason from Ghana.

"When I finally reached the camp at the port, conditions were bad. There was so much bombing, and we had to run like cats and dogs," he added.

The bombardment of the migrants' camp, like so many attacks in the war to date, exposed as a lie Gaddafi's claim that he is not targeting civilians. After two days during which the port had not been shelled, the attack appeared timed to coincide with the humanitarian ship's arrival.

Since the start of the evacuation of migrants early last month, around 15 have been killed by shells fired by Gaddafi's troops.

Despite the ongoing violence, many of the migrants on the ship were initially reluctant to leave Libya since they had endured difficult – and illegal – journeys through the Sahara desert just to get there in the first place.

Mabel Joseph, 27, who is four months pregnant, said she had only decided to return home when Libya's civil war reached the neighbourhood where she was staying a few weeks ago, leaving many dead. "I just made up to mind to leave at that time. I was making good money but I will not return. We regret ever coming to Misrata," she said.

Aslem Orubor, 28, a diesel mechanic from Nigeria, was on the boat with his wife, Maureen, and daughter Natasha, who is one. He said: "We were so scared for the two weeks we lived in the camp. Even our baby is affected by all the noise of the bombing."

Mamumuddin Bachuminia, 30, from Bangladesh, said Gaddafi's troops had ransacked his house, taking his mobile phone, television and his money. "Lots of people were killed. We need to go home," he said.

Salim el-Goumati, a 54-year-old Libyan man, reached the deck of the ship and burst into tears. Out of a small plastic bag, the only item he carried, he retrieved photos of two of his young children, aged eight and five. They were in Tripoli, with his wife and a third, smaller, child. He had not heard from them since the start of the revolution, he said. Now, his house had been destroyed by Gaddafi's missiles, and he had lost everything.

"Libyans are good people. But look what he [Gaddafi] has done to us."