Yes you can if you start early morning or previous night
My recommendation is you take previous night train which reach early morning to Gaya.
Gaya and Bodhgaya is closed, so take auto /taxi and reach Bodhgaya
you can able to see all Buddhist temples and monasteries within 2-3 hrs.
take taxi/auto and back to Gaua station and back to Varanasi
If you planning to travel by car , book car from Varanasi, start early morning, reach Bodhgaya see all temples and monasteries and back to Varanasi late evening. It may be tiring journey.
There are various trains and I think that would be more comfortable.
thank you so much...
Have awesome travel