The pitch of the sound increases because pitch depends upon frequency. When nail is hammered it produces an increasing frequency which is equal to the vibrations per second because it’s the part of the nail that remains sticking out of the wall that’s vibrating. As the nail enters the wall, that part gets shorter and the frequency of the vibration increases. This increasing frequency results in increasing pitch is directly proportional to frequency. Hence the pitch increases.
Regards