What is the use of the capacitor in this relay circuit?
That capacitor is not really needed but it does help in reducing high-frequency emissions and switching pulses which could disturb other circuits working on the same supply (that 24 V line).

Especially when a relay coil is switched off a very sharp pulse is generated (due to the magnetic flux in the relay core wanting to induce a current in the coil). The flyback diode takes care of most of this but it is possible that it is not fast enough to catch the sharpest edges of the pulse. Then the capacitor helps to smooth these edges out.