Image copyright Smithsonian Institution Image caption The scientists brought in a number of digital techniques to record the discoveries

It is one of the most astonishing fossil discoveries of recent years - a graveyard of whales found beside the Pan-American Highway in Chile.

And now scientists think they can explain how so many of the animals came to be preserved in one location more than five million years ago.

It was the result of not one but four separate mass strandings, they report in a Royal Society journal.

The evidence strongly suggests the whales all ingested toxic algae.

The dead and dying mammals were then washed into an estuary and on to flat sands where they became buried over time.

We managed to sample all the superstars of the fossil marine-mammal world in South America in the Late Miocene Nicholas Pyenson, Smithsonian Institution

It was well known that this area in Chile's Atacama Desert preserved whale fossils.

Their bones could be seen sticking out of rock faces, and the spot acquired the name Cerro Ballena ("whale hill") as a result.

But it was only when a cutting was made to widen the Pan-American Highway that US and Chilean researchers got an opportunity to fully study the fossil beds.

They were given just two weeks to complete their field work before the heavy plant returned to complete construction of the new road.

The team set about recording as much detail as possible, including making 3D digital models of the skeletal remains in situ and then removing bones for further study in the lab.

Image copyright Adam Metallo/Smithsonian Institution Image caption The skeletons are remarkably complete, having being subjected to very little scavenging at death

Identified in the beds were over 40 individual rorquals - the type of large cetacean that includes the modern blue, fin and minke whales.

Among them were other important marine predators and grazers.

"We found extinct creatures such as walrus whales - dolphins that evolved a walrus-like face. And then there were these bizarre aquatic sloths," recalls Nicholas Pyenson, a palaeontologist at the Smithsonian's National Museum of Natural History.

"To me, it's amazing that in 240m of road-cut, we managed to sample all the superstars of the fossil marine-mammal world in South America in the Late Miocene. Just an incredibly dense accumulation of species," he told BBC News.

The team immediately noticed that the skeletons were nearly all complete, and that their death poses had clear commonalities. Many had come to rest facing in the same direction and upside down, for example.

This all pointed to the creatures succumbing to the same, sudden catastrophe; only, the different fossils levels indicated it was not one event but four separate episodes spread over a period of several thousand years.

The best explanation is that these animals were all poisoned by the toxins that can be generated in some algal blooms.

Such blooms are one of the prevalent causes for repeated mass strandings seen in today's marine animals.

Image copyright Smithsonian Institution Image caption The Smithsonian has produced tools to allow the public to tour and investigate Cerro Ballena

If large quantities of contaminated prey are consumed, or the algae are simply inhaled - death can be rapid.

"All the creatures we found - whether whales, seals or billfishes - fed high up in marine food webs and that would have made them very susceptible to harmful algal blooms," said Dr Pyenson.

The researchers believe the configuration of the coastline at Cerro Ballena in the late Miocene Epoch worked to funnel carcases into a restricted area where they were lifted on to sand flats just above high tide, perhaps by storm waves.

Media playback is unsupported on your device Media caption The finds were a media sensation in 2011

This would have put the bodies beyond marine scavengers. And, being a desert region, there would have been very few land creatures about to steal bones either.

A lot of the fossils at Cerro Ballena are perfect but for a few nicks inflicted by foraging crabs.

The researchers are not in a position to say for sure that harmful algal blooms were responsible for the mass strandings. There were no distinct algal cell fragments in the sediments; such a presence could have amounted to a "smoking gun". What the team did find, however, were multiple grains encrusted in iron oxides that could hint at past algal activity.

"There are tiny spheres about 20 microns across - that's exactly the right size to be dinoflagellate cysts," said Dr Pyenson.

"They're found in algal-like mats all around the site. We can't say whether those were the killer algae, but they do not falsify the argument for harmful algal blooms being the cause in the way that the sedimentology falsifies tsunami being a potential cause."

Cerro Ballena is now regarded as one of the densest fossil sites in the world - certainly for whales and other extinct marine mammals. The scientists calculate there could be hundreds of specimens in the area still waiting to be unearthed and investigated.

The University of Chile in Santiago is currently working to establish a research station to carry this into effect.

To coincide with the publication of a scholarly paper in Proceedings B of the Royal Society, the Smithsonian has put much of its digital data, including 3D scans and maps, online at cerroballena.si.edu.

Image copyright Adam Metallo/Smithsonian Institution Image caption Hundreds of fossils await unearthing and description at Cerro Ballena

Jonathan.Amos-INTERNET@bbc.co.uk and follow me on Twitter: @BBCAmos