That is because the atmosphere of Venus is mostly Carbon Dioxide (96.5%). This results in the heat of the sun being trapped inside Venus's atmosphere due to the phenomenon of Greenhouse Effect. Thus the average temperature is 462 degree Celsius.
In comparison, the Mercury's atmosphere is almost negligible as the gases are blown away by the solar wind. Solar light pushes the atmospheric gases away from the Sun, creating a comet-like tail behind the planet. Most of its atmosphere contains particles from the solar wind other than traces of helium, hydrogen and oxygen. So the side facing the Sun can be as hot as 430 degree Celsius and side facing away can be as cold as -170 degree Celsius.
Thus, distance from the sun is not the only factor determining the temperature of a planet.