The marchers converged on the Spanish capital's main Atocha train station for the start of the festivities. The 400,000 figure was later given by police.
The slogan for this year's march was “History, struggle and memory” and participants marked 50 years since the Stonewall riots in New York in 1969 which were the foundation of the gay rights movement worldwide.
Catholic Spain at the time was controlled by dictator General Francisco Franco and homosexual acts were illegal. Thousands of gays were shipped off to rehabilitation centres or even jailed.
Homosexuality was decriminalised in 1978, three year's after Franco's death, since when Spain has become one of the most LGBT-tolerant countries in the world, authorising gay marriage and adoptions by gay couples in 2005.
“I'm 53 years old and I have suffered,” said Manuel Carmona, carrying a large rainbow flag for Saturday's march.
“But those who are 63-years-old suffered more and those who are 73-years-old even more and I want us to recognise these people who have helped us,” added Carmona who has been attending the pride march for 30 years.
Madrid's city hall swung to the right in June after a conservative candidate was elected mayor with the support of newly-emerged far-right party Vox, in a move that has caused concern in Spain.
Vox has said that the Madrid Gay Pride Parade should be moved to a park in the suburbs and opposes public subsidies for the event.
“They do not want to understand that when we talk about LGBT rights, we are talking about human rights,” said Spanish Interior Minister Fernando Grande-Marlaska, who is himself gay and took part in the march.