Sri Lanka's navy has rescued an elephant that got into difficulties after being washed out to sea, a spokesman said Wednesday, calling it a "miraculous escape".

Chaminda Walakuluge said the navy mounted the 12-hour rescue after spotting the elephant struggling to stay afloat around eight kilometres (five miles) off the island's northeast coast.

Divers aided by wildlife officials approached the distressed animal and tied ropes to it before towing it gently to shallow waters near the coast, where it was released late on Tuesday.

Walakuluge said the animal had likely got swept into the sea while crossing the Kokkilai lagoon, a large stretch of water that lies between two areas of jungle.

"They usually wade through shallow waters or even swim across to take a short cut," he said.

"It is a miraculous escape for the elephant."

Photos posted on the navy's website show the animal trying to keep its trunk above the water as divers approached it.

The rescue came six weeks after the navy and local residents saved a pod of 20 pilot whales who became stranded in nearby Trincomalee, a natural harbour that is popular for whale watching.

The waters around Trincomalee, which was used by Allied forces as a staging post during World War II, have a high concentration of blue and sperm whales, while the surrounding jungles have herds of wild elephants.