A train passing through a seaside rail station in Spain plowed into a group of people crossing the tracks, killing at least 12 and injuring 14, officials say.

A large group of people got off a commuter train that arrived in Castelldefels near Barcelona shortly before midnight Wednesday. Rather than use an underpass to get out of the station to get to a beach party marking the start of summer, about 30 people tried to cross the tracks on foot, witnesses said.

In a matter of seconds, a long-distance train that was not scheduled to stop at that station barrelled into them at high speed.

Marcelo Cardona, a Bolivian in his 30s who was on the commuter train, described the scene as hellish as people were run over. He said the passengers, racing to get the beach party around a bonfire, crossed "in a wave," but he held back and waited on the platform.

"The euphoria of getting off the train immediately became screams," Cardona said. "There were people screaming, 'My daughter, my sister.'"

Most of the victims were Latin American immigrants from Ecuador, Chile, Colombia and Bolivia. Aside from one woman in her 40s, all of the injured were 19 or younger and two are minors.

The final toll was 12 dead and 14 injured, said Nacho Solano, a spokesman for the Catalonia regional government's civil protection department. Three of the injured were in critical condition. An earlier toll put the number injured at 17.

The chairman of the state railway company, RENFE, Teofilo Serrano, said he was "almost certain" the train was not exceeding the speed limit as it travelled through the station.

The Catalan regional government declared a day of mourning and Spanish King Juan Carlos cancelled the annual celebration held in honour of his saint's day.

It was Spain's deadliest train accident since 2003, when 19 people died in a collision between a passenger train and a freight train.