What is the function of MgCl2 in PCR?

How does MgCl2 really work in the amplification of DNA? What is its role and mechanism? Thank you in advance