The weekend storm affected almost the whole country, with the heaviest rains recorded on Sunday in the coastal provinces of Cadiz, Tarragona and Castello, according to state weather office Aemet.
Two people died in the province of Toledo as a result of the storm, the head of the regional government of Castilla La Mancha said, without giving details.
Spanish media said a man was found dead by police during a rescue attempt on a road near the town of Bargas while another man died as rescuers tried to reach him in the town of Casarrubios del Monte.
Emergency services were looking for a man who went missing after his car was swept away early on Monday by a swollen river in the rural area of Aldea del Fresno west of Madrid, a spokesman from Madrid’s emergency services, Javier Chivite, told public television RTVE.
Firefighters found his 10-year-old son — who was also in the car and was initially reported as missing — on Monday on top of a tree, he added.
Emergency services had rescued the boy’s mother and sister earlier in the day.
“The poor boy spent the night perched in a tree,” the head of the regional government of Madrid, Isabel Diaz Ayuso, told reporters.
The family, who live in the Madrid suburb of Alcorcon, were staying at a holiday home they own in Aldea del Fresno when the storm hit. They took to the road because they became alarmed by the flash flooding, she added.
Several bridges collapsed in Aldea del Fresno and torrents of water swept away many cars, according to an AFP reporter at the scene.
‘Behave with caution’
On Sunday residents of the Madrid region received an emergency text accompanied by a loud alarm urging them not to use their vehicles and stay at home. It was the first time the authorities had used this mobile phone alert system.
Several theatres were closed and Sunday’s football match between Atletico Madrid and Sevilla was suspended.
A number of metro lines were closed in Madrid on Monday morning due to flooding caused by heavy overnight rains.
High-speed rail links between Madrid and the southwestern region of Andalusia and the eastern coastal region of Valencia, which closed on Sunday, reopened on Monday although trains were running at slower speeds in some sections, railway operator Renfe said.
The heavy rainfall eased on Monday morning. Aemet lowered its alert level for the Madrid region to yellow from a maximum red alert on Sunday.
Spanish Prime Minister Pedro Sanchez thanked emergency services for their work and urged people to “continue to behave with caution”.
The torrential weather comes after Spain endured an intense heatwave and persistent high temperatures in August.
Scientists warn that extreme weather such as heatwaves and storms is becoming more intense as a result of climate change.