About 3,000 protesters marched from Atocha railway station to the central Plaza de la Provincia behind a banner declaring "Sahara Free now!", a photographer with news agency AFP said.
Banners from many parts of Spain could be seen at the demonstration which also included live music and street performers dressed in the red, green, black and white of the traditional Saharawi flag.
"We believe the Saharawi people have to right to self-determination," said Jose Taboada, president of the Coordination of Spanish Associations of Solidarity with Western Sahara, which organised the protest and wants the territory to vote on independence.
"This is a scream to demand that the Spanish government intervene to help the Saharawis to be able to vote to decide what they would like to be," he told AFP.
Western Sahara, larger than Britain but with a population under one million people, has lucrative phosphate reserves, rich fishing grounds and, potentially, oil.
Morocco took control of most of the territory in November 1975 when colonial power Spain withdrew, prompting a guerrilla war for independence that lasted until 1991 when the United Nations brokered a ceasefire and sent in a peacekeeping mission.
The agreement also directed a referendum in a few months' time, under which Saharawis would vote either for independence or to integrate with Morocco, but that vote has yet to take place.
Local Saharawi people have long campaigned for the right to self-determination but most Moroccans now view the territory as a part of their kingdom.
Morocco wants talks about self-rule for the territory under Moroccan sovereignty, but the territory's Algerian-backed Polisario Front independence movement has demanded a referendum that would include the option of full independence.
Several thousand Saharawis live in refugee camps across the border in western Algeria's barren Tindouf province or in Moroccan controlled territory where they complain of repression.
The protest was staged to coincide with a European conference for support for the Saharawi people held in Madrid.
The conference wrapped up with a resolution by the participants accusing Morocco of preventing the United Nations' envoy for Western Sahara, Christopher Ross, of carrying out his mission.
"The conference renews its support for Christopher Ross and recognizes his determination to carry out his mission," the resolution added.
Morocco in 2012 said it had lost confidence in Ross, claiming he made "biased and unbalanced" decisions concerning the disputed region.
"The Polisario Front and the Saharawi government until now are convinced by the Saharawi public opinion of the need to continue on a peaceful and diplomatic path," the head of the Polisario, Mohamed Abdelaziz, told AFP on the sidelines of the conference in Madrid.
"The situation in the region is dangerous, with a real increase in the causes of instability as the situation in Libya, Mali and the Sahel region in general show," he added.
"The Saharawi republic and the Polisario Front are necessary forces to protect stability and moderation, to protect the region against a slide towards extremism," he added.
"The one who has chosen war and escalation and extremism is, unfortunately, Morocco."
Europe and the United States have expressed concern that the conflict, because it has soured relations between Morocco and Algeria, is preventing the two neighbours from working together to contain Islamist violence in the region.