It seems that in Spain there are two separate police forces under the authority of Madrid: the Guardia Civil and the Policia Nacional. Why is that? And what is the difference between the two?
It's a bit like Police vs Gendarmes in France. One is in charge of cities, the other of countrysides:
The National Police Corps (Spanish: Cuerpo Nacional de Policía, CNP; [ˈkwerpo naθjoˈnal de poliˈθi.a]) is the national civilian police force of Spain. The CNP is mainly responsible for policing urban areas, whilst countryside policing is generally the responsibility of the Civil Guard, the Spanish gendarmerie. The CNP operates under the authority of Spain's Ministry of the Interior. They mostly handle criminal investigation, judicial, terrorism and immigration matters. The powers of the National Police Force varies according to the autonomous communities, Ertzaintza in the Basque Country, Mossos d'Esquadra in Catalonia, and Policía Foral (Foruzaingoa) in Navarre are the primary police agencies while BESCAM in the Madrid region is more of a resources provider. In Andalusia, Aragon, Asturias, Galicia, and Valencia the National Police units are functionally acting directly under the orders of the Autonomous Communities to which they are attached.
The Guardia Civil (English: "Civil Guard"; [ˈɡwarðja θiˈβil]) is the oldest law enforcement agency in Spain. It is organised as a military force charged with police duties under the authority of both the Ministry of the Interior and the Ministry of Defence. The corps is colloquially known as the benemérita (reputable). In annual surveys, it generally ranks as the national institution most valued by Spaniards, closely followed by other law enforcement agencies and the military. It has both a regular national role and undertakes specific foreign peace-keeping missions. As a national police force, the Guardia Civil is comparable today to the French Gendarmerie, the Italian Carabinieri, the Portuguese National Republican Guard and the Dutch Royal Marechaussee as it is part of the European Gendarmerie Force.
As part of its daily duties, the Guardia Civil patrols rural areas (including highways and ports) and investigates crimes there, whilst the Policía Nacional deals with safety in urban situations. Most cities also have a Policia Municipal. The three forces are nationally coordinated by the Ministry of the Interior. The Guardia Civil is usually stationed at casas cuartel, which are both minor residential garrisons and fully equipped police stations.
One of the main differences is that the Guardia Civil is not exactly a police corp, they are part of the army.
Although in practice they are another police corp, being part of the army means they have a different regime for working rights and so on. For example the civil guard does not have the right to join a union and defend their rights as workers (they have an association, but it is not a union).
Also as It has been mentioned in another answer, the civil guard is normally in charge of rural areas while the Policia Nacional is in charge of cities.