Fairly certain that your problem is related to a loose fitting pipe on your water line somewhere that is not properly clipped or strapped.
Simply, when you close a tap under mains pressure the shock wave causes a vibration we call water hammer within pipes on ocassions. This is usually more commonplace with appliances that have fast opening or closing solenoid valve that “slam” shut causing the shock wave in the piping system. If you cannot locate the loose pipe you can fit a device into the water line called a water hammer arrestor that will do the job for you.
You should also check and make sure that the water heater has a pressure limiting valve fitted on the inlet cold water line to it – the purpose of this device is to ensure that water pressure does not exceed that to which the water heater relief valve has been set, thereby no stressing any other valves within the system.
Selgas Services Ltd
Craftsman Gasfitters, Plumbers, Electrical Service Technicians