Usually this is from something called water hammer. If water is turned off quickly, it sends a pressure spike through the plumbing making it rattle or shake for a moment. This is often from a faulty ball cock in one of the household toilet tanks or a faulty pressure regulator. Although it is generally not an emergency, if it happens often it can cause a leak. If necessary, contact a plumber for help.