Upvote:19
As a general principle, the entire Schengen area is considered one country for immigration purposes. This means in particular that a flight between two Schengen countries (in your case, Spain and Italy) is considered a domestic flight and there is no immigration control before or after the flight.
Hence, in your case you will enter the Schengen area and pass immigration when you arrive in Madrid, regardless of whether you exit the airport or not. There will be no immigration control in Italy at all.