During an earthquake, it is very likely that a gas pipe will crack or obtain damage. Natural gas can be harmful to inhale leading to health concerns, but more concerning is its ability to catch fire. An earthquake valve is designed to be installed in regions where earthquakes are more likely, such as California. It is usually installed between the gas meter and the house. The way they work is much simpler than you would imagine. There is a ball that is sitting on a pedestal over the flow pipe, when the valve shakes it knocks the ball off the pedestal blocking the flow.
There are several types of valves but most can be reset by pulling a chain or turning a screw, however, it's recommended to have a plumber present when resetting it after an earthquake to ensure there are no leaks.
The downside to having one is that if something bangs into the gas pipe, such as a garbage can, or even kids playing, it can trip the mechanism. This not only shuts off the gas to your house, but you will have to relight all your pilots. Thankfully most new appliances are moving away from pilots, but new water heater tanks still have them. To reduce the chance of this happening, you can install bollards, or fencing around the device.