According to local officials, the Rodalies train collided with a retaining wall which fell onto the track between Gelida and Sant Sadurní on Tuesday evening.
Catalonia regional fire Inspector Claudi Gallardo said all passengers had been removed from the train.
The incident occurred as heavy storms battered north-eastern Spain, as coastal areas in the east and north-west of Spain are on high alert because of the weather.
Eleven ambulances were on the scene in Gelida, Catalonia - around 35km (21.7 miles) west of Barcelona - treating those injured, emergency services said.
The local fire service said 35 crews had been sent to the area and one passenger trapped inside the train had been rescued.
In a later update it said that no-one remained inside and that its crews were conducting a sweep of the area to rule out any further victims.
Emergency services said they had evacuated some of the injured to nearby Moisès Broggi, Bellvitge, and Vila Franca hospitals.
As well as the five seriously injured people, six were in a less serious condition and 26 were in a "mild" condition, emergency medical services said.