Praise be to Allaah.
If that is by doing something permissible, such as reciting du’aa’s that are prescribed or permitted in Islam, or by using a ruqyah that is prescribed in Islam, there is nothing wrong with that. But if a person learns magic in order to counteract magic, or for any other purposes, then that is not permitted and in fact is something which nullifies a person’s Islam, because it is not possible to learn it without falling into shirk. That happens by worshipping the shayaateen (devils), by offering sacrifices to them and making vows to them, and other kinds of worship, and offering sacrifices to them and seeking to draw close to them with things that they love, until they serve a person and do what he wants. This is the mutual benefit which Allaah refers to in the aayah (interpretation of the meaning):
“And on the Day when He will gather them (all) together (and say): ‘O you assembly of jinn! Many did you mislead of men,’ and their Awliyaa’ (friends and helpers) amongst men will say: ‘Our Lord! We benefited one from the other, but now we have reached our appointed term which You did appoint for us.’ He will say: ‘The Fire be your dwelling place, you will dwell therein forever, except as Allaah may will. Certainly your Lord is All-Wise, All-Knowing.’”