Upvote:2
The answer to your question depends slightly on whether your transit in Italy is between two non-Schengen flights or not. If so, you can almost certainly leave the airport. If not, you can definitely leave the airport.
(Government authorities can always deny entry or even detain you if your documents are out of order, or if you are wanted by the authorities, or you otherwise represent some kind of threat. This answer assumes that none of these exceptional conditions applies to you.)
If you are arriving from a non-Schengen airport and departing to a non-Schengen destination, you can only leave the airport if the Italian immigration authorities permit you to pass through passport control, which they ought to do, because your D visa permits you to visit other Schengen countries for up to 90 days in any 180-day period.
If you are arriving from a non-Schengen airport and departing to a Schengen destination, you will have to clear Italian immigration no matter what in order to reach your flight, so you will be able to leave the airport once you've done that. In such a case you will not clear immigration controls after your next arrival because your second flight is an internal flight. If you are in transit to the country issuing your D visa then the immigration authorities have reduced discretion to refuse entry, but they could still do so in exceptional cases.
If you are arriving from a Schengen airport and departing to a non-Schengen destination, you will have to clear Italian exit controls no matter what in order to reach your flight, so you will be able to leave the airport before you do that. In such a case you will not have cleared exit controls before your previous departure because your first flight will have been an internal flight. The authorities will prevent you from leaving only if there is some suspected irregularity.
If you are arriving from a Schengen airport and departing to a Schengen destination, you will not clear any passport control anywhere. You will not encounter an Italian immigration checkpoint and the Italian immigration authorities will have no say in the matter; you will be able to leave the airport no matter what.