On the most basic level, scratching is a way to remove whatever irritant might be causing the itch in the first place. If an insect is biting you, or you have a patch of dead, flaking skin, scratching has a good chance of getting rid of it. In this case, it's pretty obvious why the scratching works.
Much of the time we scratch, however, it seems to be somewhat after the fact. Perhaps the mosquito has already bitten us and flown away, and we are left scratching at the swelling bite. Clearly this won't get rid of the mosquito. Is there any good reason for this kind of scratching, or is this just an over-zealous instinct that doesn't realize it's too late?
Actually, this kind of scratching can serve two additional functions. First, it can stimulate the irritated tissues and cause extra blood to flow to them. This can help your immune system remove whatever itchy substances might be below your skin. Second, it can trick your brain into ignoring the annoying itch. Your nervous system can generally process only one stimulus from a given area of your skin at a time. If you scratch at an area that itches, this new, greater stimulus can override the itching, effectively blocking it out.