You bought off the black market. The more you buy, the more likely a patrol will try to scan you.
There is also a way to look at how suspicious your overall transactions are and how likely a patrol will pursue you. If you hover your mouse over the black market icon, at the bottom of the tooltip it was say "suspicion level: none" at first but as you buy or sell off the black market it will change to "suspicion level: low" - "suspicion level: medium" - "suspicion level: high" etc.
The patrol chasing you is RNG, but this rating affects how likely or unlikely it will happen. Reputation is also a large factor here. So if you are friendly with a faction, even a "suspicion level: high" transaction is far less likely to provoke a patrol into pursuing and scanning you.
Also keep in mind that even if they scan you there is a chance that the patrol won't find your contraband and you won't lose any rep. If they do find it and the fleet is weaker than yours, I think you can simply refuse to hand it over for a rep loss - or just refuse the scan in the first place.
*EDIT*
Also, during the transaction as your suspicion raises you can somewhat lower it back down again by also making transactions on the regular market to "cover" your black market dealings with legitimate business. It takes a disproportionate amount of legitimate trade to do this. One thing I like to do is purchase black market goods after a salvage run because I typically have a large quantity of metals to cover the black market transaction by selling the metals on the normal market.