Upvote:3
The overlapping rules and institutions are somewhat confusing (and the comments a little bit too) but here is how it works:
The European Union does not have a unified border or visa policy. EU citizens enjoy extensive freedom of movement rights within the EU but that does not mean other people do or that there can be no border controls between EU countries. As a US citizen, you are what's called a “third-country national” in EU lingo and EU rules do not directly concern you for this purpose.
The Schengen area is a border-free area with a single visa policy for short stays. It's tightly linked with the EU but not fully coextensive with it. Precisely, all EU countries except the UK and Ireland are supposed to join the Schengen area eventually but Cyprus, Romania, Bulgaria and Croatia haven't done it yet. And all other EU countries (and some non-EU countries like Norway and Switzerland) are already part of the Schengen area.
Border checks have been lifted between Schengen countries so if you go from one Schengen country to the other, you don't have to go through passport control or get your passport stamped. On the other hand, if you leave a Schengen country (say Spain) to go to a non-Schengen country (including the UK), you will have to go through a Schengen exit control.
But but but… international airports in the Schengen area (and the UK, Turkey, the Middle East…) work differently than in the US. If you don't need to leave the transit area of the airport, come from a non-Schengen country and go to another non-Schengen country, the visa requirements are different and you generally don't have to formally enter the Schengen area and complete the entry formalities. In simple terms: No passport control for your trip.
Finally, no matter what the theory says, spot checks can happen everywhere. They happen increasingly frequently on land borders within the Schengen area (in ways that are becoming difficult to defend legally speaking but that's another question…) and they also happen at airports. In that case, if there is no proper passport check area to reopen temporarily, police officers can also simply stand right out of the jet bridge and ask passengers to show their documents before they actually enter the airport building (this happened to me so I can vouch it does exist).
In that case, it's not an “external” border check, which means that the police asks people to show an ID or passport (and, as applicable, visas or residence permits) but should not stamp them. I don't think that's very common and I would expect that to be even less likely in Madrid for a flight coming from the US but that's pure guesswork on my part.