Precautions:
- Keep some spare cash, credit card, and ID separate from what you are carrying, if possible.
- Carry a photocopy of your passport and other important documents with you, separate from the originals.
- Leave copies of these important documents with a dependable friend or relative. (Mouviciel's idea of using dropbox is great.)
- Register with your country's embassy/consulate so that they have your info.
What to do if it happens:
- Report the problem to your airline if you think you will miss your flight.
- Report to the police and get a police report.
- Visit your country's embassy or consulate and get help.
Anecdote
Something similar happened to me: I was mugged (jumped) and my passport, money, and other forms of ID were stolen. My backpack was stashed elsewhere, but unfortunately I hadn't left any of these valuable things in my backpack, since I didn't know whether that location was safe.
Losing not only my passport but also my money and ID revealed the problem that it is nearly impossible to get anything done with neither an ID nor any money! To get around this problem, my relative wired money (via Western Union) to someone I had met just a few days earlier in my travel (a trusted stranger).
The day after the mugging, I filed a report at a police station, and then went to the U.S. embassy with my sad story. I had a relative fax them a copy of the identification page of my passport. Based on this, the police report, and my rather convincing black eye, they issued me a temporary passport within a few hours for the usual fee of about $60 and passport photos taken immediately prior. This temporary passport had a validity of 1 year, and contained the restriction that, while it could be used for travel, it could not be used as proof of identity or citizenship.
I was traveling overland, so I didn't have any booked flights to deal with.
Amusingly, I later had this passport validated as proof of identity and extended to a full 10 years validity. You can imagine that I had to tell the whole story every time I crossed a border and the inspectors saw my ridiculous post-mugging photo.