Eleven sailors mostly from Asia held hostage for almost four years by Somali pirates have escaped their captors and are safe in Kenya.

The sailors, who had been held in dire conditions and suffered beatings and torture, included seven men from Bangladesh, one Indian, one Iranian, and two from Sri Lanka.

John Steed, a former British army colonel who spent years trying to negotiate their release, said on Saturday the men "sneaked out a window" to escape their captors.

"It is great news that they are at least free ... given what they have been through, they are all in good health," he said after arriving safely in Kenya with the men on a special flight from Somalia.

After escaping through a window from their pirate prison, the men were rescued by security forces from the northern Somali Galmadug region, Steed added.

Their boat, the Malaysian-flagged container ship MV Albedo was captured in November 2010 but sank in rough seas last July.

During their captivity, one Indian colleague was shot by the pirates in an argument, and four others from Sri Lanka drowned.

Seven other Pakistani crew members were released in 2012 after a businessman paid their ransom but those remaining could not afford the hefty demands of the pirates.

"The crew members and their families have suffered unimaginable distress," United Nations special envoy to Somalia Nicholas Kay said in a statement.

"The crew underwent the trauma of piracy, their ship sinking, and then being held ashore in very difficult conditions."

The United Nations said they had been handed over to its care, and "will be repatriated to their home countries over the coming days".

The sailors, like 38 others from different boats who remain captive, were abandoned by their ship's owner whose willingness to pay to free them sank along with their boat.

"While we have seen a significant reduction in piracy off the coast of Somalia in recent years, I remain deeply concerned that 38 other crew members are still being held hostage," Kay added.

Pirate attacks off Somalia have been slashed in recent years, with international fleets patrolling the Gulf of Aden and Indian Ocean, as well as armed guards being posted aboard many vessels.

At their peak in January 2011, Somali pirates held 736 hostages and 32 boats, some onshore and others on their vessels.