Sometimes an itch is caused by an irritant on the skin, which causes nerves to send a signal to the brain that there is something wrong at that location. Scratching the itch sometimes overwhelms the nerve endings with signals and causes the itch to go away. (This is what usually happens when we feel itchy).
Sometimes, however, the itch happens from the other end - there is a misfire or false signal in the brain at the location where a nerve signal would be received, and we interpret that as a signal coming from the nerve, where there is actually nothing at the other end to cause a signal to be sent. However, scratching the location where we perceive the itch to be coming from can also overwhelm the nerve endings with signals, which can send signals to the brain and overwhelm the receiving end and cause the itch to go away.
Source: had a neurologist (¿ I think ?) girlfriend who did a study on phantom pain in amputees. They could stimulate bits of the brain and “trick” people into feeling their phantom pain somewhere else where it could be treated, or feeling itchy, sometimes on existing limbs; sometimes the itch would be on the non-existent amputated limb.