An itch is a stimuli on the nerve endings under your skin, the same ones that send pain signals to the brain.
As soon as we feel an itch, our first natural response is to scratch the spot of the itch with our fingernails. The reason for this response is simple -- we want to remove the irritant as soon as possible. Once you've scratched the area of irritation, you are likely to feel some relief. When your brain realizes that you've scratched away the irritant, the signal being sent to your brain that you have an itch is interrupted and therefore no longer recognized by the brain.
Even if you don't remove the irritant, scratching will at least cause pain and divert your attention away from the itching. The irritant that caused the itching is very small, maybe only a few microns in length, so it disturbs only a few nerve endings. When you use your fingernail to scratch the spot where the irritant is, you not only remove the irritant but you irritate a lot more nerve endings than the irritant.