Because the bolt carrier group comes to a very sudden stop (think "slams shut") when the bolt locks into the barrel extension, it tends to bounce backward a bit (see "bolt carrier bounce") and the bolt unlocks a bit from the barrel extension.
This is undesirable, particularly in fully automatic fire (which the original AR-15 was capable of), because the hammer (which is falling for the next shot) might strike the firing pin while the bolt carrier group is still slightly to the rear and unlocked, which would result in a light or no primer strike and no ignition.
That would suck to get a "click" when you really needed a "bang".
As the buffer body moves forward, the loose weights tend to stay at the rear of the body due to inertia. When the buffer body stops its forward motion because the bolt has locked into the barrel extension, the weights move toward the front of the buffer body due to inertia.
Because the loose buffer weights are still moving forward while the bolt carrier group has come to its forward stop, they meet the front of the buffer body about the time the bolt carrier group is trying to bounce back. The force of the loose weights meeting the front of the buffer body causes the buffer body to nudge the bolt carrier group forward, helping to negate bolt carrier bounce and the possibility of the hammer hitting the firing pin while the bolt is slightly unlocked.
The sliding weights are meant to address bolt carrier bounce in automatic fire (although some have suggested that it also helps semiauto rifles that are heavily gunked up as well).