Both Ice and Heat act as counterirritants.
This means that the sensation of either reduces the brains recognition of pain. This reduces pain and muscle spasm. They have different affects on the circulation.
Ice closes blood vessels so it reduces swelling and bruising while heat opens blood vessels. This improves blood supply and aids in the healing process.
Therefore early after injury Ice is essential. Once healing has started heat is applied.