You clear inmigration in MAD, but you don't retrieve your luggage there. As you come from outside the EU your luggage will have a white tag (luggage inside the EU has a green tag), so when you claim your luggage in AGP the custom officers may ask you to open it as they'll be able to identify that you come from a country outside the european borders