It's called "spark knock" or "detonation," and is a form of abnormal combustion caused by excessive heat and pressure in your engine's combustion chambers and/or using fuel with too low an octane rating.
The metallic knocking sound is actually produced by shock waves inside your engine's combustion chambers. When a spark plug ignites the air\fuel mixture in a cylinder, it produces an explosion that spreads outward from the point of ignition like an expanding balloon. The outer edge of this balloon of fire is called the flame front. During normal combustion, there's a single flame front that expands and consumes all the air and fuel in the cylinder until combustion is complete. Pressure rises gradually then falls off producing a smooth power stroke. But when there's too much heat and pressure for the octane rating of the fuel, little pockets of air and fuel can ignite spontaneously when the initial flame front begins to burn. As these multiple flame fronts expand and collide with one another, the collisions produce shock waves that cause the sharp, metallic pinging or rattling sound you hear.
Mild detonation can occur in almost any engine and usually causes no harm. But prolonged heavy detonation can be very damaging to your engine. Because detonation causes such a sudden rise and increase in combustion pressure, it's more like a hammer-blow than a steady push. If the problem persists and is not corrected, it can damage the spark plugs, head gasket, pistons, rings and rod bearings.
Detonation also causes the engine to lose power because the cylinder pressure peaks too quickly for an efficient power stroke. Detonation can also raise cylinder temperatures to the point where another form of abnormal combustion called "preignition" occurs.
Preignition is when the fuel is ignited by a hot spot in the combustion chamber or ignites spontaneously from excessive heat. Preignition can also be very damaging because the fuel ignites before it is supposed to. The instant at which ignition occurs must be precisely timed for efficient combustion. If the fuel starts to burn on its own accord before the spark plug fires, fuel economy and performance plummet while exhaust emissions soar. What's more, preignition can burn a hole right through the top of a piston!
One way to reduce the risk of detonation is to use a higher octane fuel.
If switching to a premium grade of gasoline fails to eliminate your detonation problem, it means something else is amiss. The causes of detonation include anything that raises combustion temperatures or pressures, or anything that leans out the air/fuel mixture. These include: