Railway authorities in Catalonia are investigating after a herd of 17 goats made their way into a train station and onto the railway tracks in the city of Terrassa, on Tuesday morning.

Surprised people snapped photos and posted videos online as they watched the goats delicately pick their way along the tracks.

The goats were "completely disoriented and frightened," security guard Antonio Jesús Martínez told El País. Using a whistle and his voice, he spent 40 minutes guiding the goats towards an unused track in the station. "It was like I had been herding goats all my life," he joked.

The goats were first spotted in a tunnel just before the station, said Jordi Carrera of Ferrocarrils de la Generalitat de Catalonia (Railways of the Generalitat of Catalonia). "Now we're trying to figure out exactly where they would have entered from."

Station staff have seen the occasional cow wander onto their outdoor tracks, he said, from a nearby farm. "But never anything like this."

He stressed that train service was not interrupted. The goats were kept in the unused track while staff waited for police to locate the goatherd. He arrived, visibly nervous, about an hour after his goats and worked with staff to come up with a plan on how best to get the animals out of the station. "We took them out one by one," said Carrera.

The last goats were led from the train platform, up the station steps and out the exit after nightfall when train services were less frequent. "Some of them ended up spending eight or nine hours in the station," said Carrera, adding: "But they were well looked after."