Riot police and firefighters were deployed to Lavapiés, a district in the centre of the Spanish capital with a large immigrant population, as angry protesters set fire to dustbins and a motorbike, and threw stones at security forces.
Demonstrators told AFP they were protesting in support of Mmame Mbage, a street vendor in his mid-thirties from Senegal, who arrived in Spain by boat 12 years ago.
Emergency services said Mbage was found unconscious on a street in Lavapiés by police on patrol.
"They were busy trying to revive him" when emergency workers arrived, a spokeswoman said. However he died of cardiac arrest.
She did not know what had happened to Mbage before he collapsed, but several other street vendors who were with him said he had been chased by police from Puerta del Sol.
"Municipal police arrived and chased him from Sol to Lavapiés with a motorbike," said Modou, a 25-year-old vendor from Senegal who refused to give his surname.
"At the end he died here," Modou told AFP, with others confirming the account.
Mbage worked as an illegal vendor and sent some money back to his family, one of thousands of migrants who have reached Spain over the years in search of a better life.
Spain is the third busiest gateway for migrants coming to Europe, with close to 23,000 arrivals in 2017. Hundreds have died along the way.