When a pipe is suddenly closed at the outlet (downstream), the mass of water before closure is still moving, therefore building up high pressure and a resulting shock wave. and also attempts to create a vacuum that may cause the pipeline to collapse or implode.
So to avoid this problem a Hammer Guard is used in the system to absorb those shocks to make the system safe.