The first recorded steam engine was designed by Heron of Alexandria in the first century AD and was known as an aeolipile.
The Roman author Vitruvius describes one:
“Æolipylæ are hollow brazen vessels, which have an opening or mouth of small size, by means of which they can be filled with water. Prior to the water being heated over the fire, but little wind is emitted. As soon, however, as the water begins to boil, a violent wind issues forth.”
Heron himself describes how to make one:
PLACE a cauldron over a fire: a ball shall revolve on a pivot. A fire is lighted under a cauldron, A B, (fig. 50), containing water, and covered at the mouth by the lid C D; with this the bent tube E F G communicates, the extremity of the tube being fitted into a hollow ball, H K. Opposite to the extremity G place a pivot, L M, resting on the lid C D; and let the ball contain two bent pipes, communicating with it at the opposite extremities of a diameter, and bent in opposite directions, the bends being at right angles and across the lines F G, L M. As the cauldron gets hot it will be found that the steam, entering the ball through E F G, passes out through the bent tubes towards the lid, and causes the ball to revolve, as in the case of the dancing figures.
If Heron had combined this with the Diolkos railway that had been in operation since the sixth century BC transporting boats across the Corinth Isthmus in Greece, which was powered by slaves, the first steam railway may have come a lot earlier.