Here's my theory.
The shell pigment on a non-speckled brown egg is evenly distributed over the entire egg shell. On speckled eggs, the pigment (for whatever reason) is not evenly distributed which results in speckles. These speckles can be flicked off with your thumb nail, incidentally. The pigment is added to the egg shell as it passes through the chicken's reproductive system. I believe there is an organ that creates the pigment and applies it at the proper time.
I think it's genetic. I've got 2 Australorp hens that consistently lay speckled eggs while the other four lay non-speckled brown eggs. None of my Orpingtons lay speckled eggs. My 3 Plymouth Rocks lay pink/light brown eggs, with the BRs laying eggs that start out light on the large end and gradually darken toward the small end.
Chickens are amazing.