First we buried a rubber ball in a jar of uncooked rice and shook it back and forth. The ball came up to the surface. In any mixture of solids there are spaces in between the pieces, or grains. When the mixture is shaken the grains have an opportunity to rearrange themselves. Since the entire mixture is subject to gravity, there is a tendency for the grains to settle downward.
Some spaces are larger than others.
If there are different size grains the smaller ones are most likely to find a space to settle into. This is because the small grains may settle into both small and large spaces, but large grains can only fit into the larger spaces. The rubber ball and rice mixture is an extreme case with one grain much larger than the others. As you shake the jar the individual rice grains have spaces to settle into, but there is never a space large enough for the ball. The shaking causes both rice and the ball to move back and forth but also up and down a bit.