The two beached whales were in distress. Rescuers had tried without success to push them back out to sea. It looked as if they would have to be put down. Then Moko the bottlenose dolphin appeared. In an incident that amazed conservationists, the dolphin – apparently responding to the whales' distress calls – led them to safety.

The pygmy sperm whales, a mother and a male calf, had become stranded on Mahia Beach, about 300 miles north-east of Wellington, New Zealand, on Monday. A local man who found them alerted his neighbour, Malcolm Smith, a Department of Conservation worker. Mr Smith and other rescuers spent an hour and a half trying to refloat the whales, only to see them beach repeatedly on a large sandbar just offshore.

"They kept getting disoriented and stranding again," Mr Smith said. "They obviously couldn't find their way back past [the sandbar] to the sea."

Download the new Independent Premium app Sharing the full story, not just the headlines

He was beginning to contemplate killing the pair, to save them from a slow, painful death, when Moko – as local people have named the dolphin – arrived. Moko, who often plays with humans at Mahia, approached the whales and guided them 200 yards along the shoreline and out through a channel into the open sea.

"Moko just came flying through the water, and pushed in between us and the whales," said Juanita Symes, one of the rescuers. "She got them to head towards the hill, where the channel is. It was an amazing experience. The best day of my life."

Mahia Beach, on the east coast of the North Island, is notorious for whale strandings. About 30 whales beach there every year, and most have to be killed. No one in the area has ever seen a dolphin swim to the rescue before.

"I've never heard of anything like this before," said Mr Smith, who has been in his job for 30 years. "The things that happen in nature never cease to amaze me."

Witnessses said that Moko and the whales could be heard making noises, apparently to each other. The latter were emitting whistles and clicks, which Mr Smith speculated were distress signals. "What the communication was, I do not know, and I was not aware dolphins could communicate with pygmy sperm whales, but something happened that allowed Moko to guide those two whales to safety."

Moko has become famous at Mahia for her antics, which include playing in the surf with swimmers, approaching boats to be patted, and pushing kayaks through the water with her snout. Scientists believe she has become isolated from her pod, and has made the beach area her home.

Mr Smith said: "It was looking like it was going to be a bad outcome for the whales, which was very disappointing, and then Moko just came along and fixed it. I pushed them out to sea two or three times, and they were very reluctant to move offshore.

"I was starting to get cold and wet, and they were becoming tired. I was reaching the stage where I was thinking it's about time to give up here, I've done as much as I can. I was close to putting them out of their misery."

Mr Smith said that it was quite possible the dolphin had heard the whales calling. "The whales were sitting on the surface of the water, quite distressed," he said.

"They had arched their backs and were calling to one another, but as soon as the dolphin turned up, they submerged into the water and followed her.

"The whales made contact with the dolphin, and she basically escorted them about 200 metres parallel with the beach, to the end of the sandbar. Then she did a right-angle turn through quite a narrow channel and escorted them out to sea.

"She obviously gave them enough guidance to leave the area, because we haven't seen them since."

Moko returned to the beach soon afterwards, and resumed her water games with the locals.

Mr Smith said the dolphin's actions – further evidence of the species' friendly, altruistic nature – had probably meant the difference between life and death for the whales. "It was amazing," he said. "It was like she grabbed them by the flipper and led them to safety."

Anton van Helden, a mammals expert at New Zealand's national museum, Te Papa, said dolphins had "a great capacity for altruistic activities". They had been known to protect people lost at sea, and were also very playful.

"We've seen bottlenose dolphins getting lifted up on the noses of humpback whales and getting flicked out of the water, just for fun," he said.

"But it's the first time I've heard of an inter-species refloating technique. I think it's wonderful."