So, scientists were surprised when they found that Mercury has a molten core. Since small planets (like Mercury) cool rapidly, the core should've frozen a while ago.
In a nutshell, though, whether or not the core stays molten over time depends on the composition of the core...as this website says,
Maintaining a molten core over billions of years requires that it also contain a lighter element, such as sulfur, to lower the melting temperature of the core material.
Mercury's core is also known to have an impressively high iron content. The interior structure of Mercury is shown below...the core is a significant portion of Mercury.
Then, for why Mercury's surface temperature can change - think of Earth, real quick. We have a molten core too, but it can get pretty cold or hot on Earth's surface...and that's with a stable atmosphere that helps regulate things and our prime position (not to far and not to close to the sun). The temperature difference would be even wilder on Mercury where there isn't a protective atmosphere and it's really close to the Sun.
Hope this helps! This website has a lot of useful information about Mercury.