How the Grayscale images are converted back to RGB?
It is not possible to get color back. A grayscale image can be converted to RGB image, by copying the grayscale data to all three RGB channels,but it will still be grayscale.

Note that in general, RGB is not simply averaged to grayscale, but a weighted average is used to match eye sensitivity.