In locations prone to quicksand like canyons, tidal areas and marshes, it is a good idea to carry a long stick. It will serve dual purpose as a wedge to help you leverage yourself free from the quicksand, and as a probe for the ground ahead of you so you do not get stuck in the first place.
Don't panic! You will likely not sink below your knees, but if you are thrashing about you will certainly sink deeper and faster. Lie on your back and backstroke to safer ground while slowly kicking your feet free. Relax and keep calm, move slowly and deliberately.
Ditch your backpack if necessary.
Take breaks. Removing yourself from quicksand is a slow and tedious process, and you do not want to completely exhaust your body.
Warn future hikers. Scratch a warning on a rock or tie a strip of bright colored bandanna to a tree before the danger area.