Most likely you had a wheel slide out from under you and then when it regained traction, the bike's momentum wasn't pointed in the same direction as the wheels. This usually causes a "high-side". They can be caused by sand, oil, excessive power, excessive braking. You get the picture. If you do a google video search you can watch lots of idiots, and a few unlucky people, wreck motorcycles and get a feel for what can go wrong. High siding is bad because it tosses the rider over the bike and then dumps him out hard, as you now know.
Always wear all the gear, and never ride on sand. If you have to ride on sand, do it like it's ice. Glad you came out pretty close to OK.