|Left: a piston shown inside of an internal combustion engine, the modern version of the gunpowder engine. Right: a typical modern piston shown with its connecting rod.|
In c.230BC, the piston, supposedly, was invented by Ctesibius, in the form of some type of air pump or bellows:
“It is said that Ctesibius invented the piston and cylinder before 200BC.”— Richard Kirby (1956), History of Engineering (pg. 154) 
“Ctesibius had been responsible, about -230, for a simple and fundamental machine, the piston air-pump, known from the descriptions of later mechanicians. This simplest of pumps entered upon a new incarnation in the +17th century, when the virtuosi began to explore with excitement the properties of vacuous spaces, for what had been invented originally as a bellows for pumping air into something now found fresh employment as the ‘air-pump’ for getting as much air as possible out of it.”— Joseph Needham (1987), Science and Civilization in China: Volume 5 (pg. 555) 
In c.1495, Leonardo da Vinci designed an upside-down cannon barrel, fitted with a piston, as sketched by him below (left), which had a rod attached to the bottom of the piston, which he stated could be used to lift a weight of large size, if gunpowder was ignited inside the cannon, therein consuming the oxygen inside of the working body volume, thereby making a vacuum, which, owing to the force of the atmospheric pressure, would lift the weight: 
In 1632, Galileo, in his Two New Sciences, gave an illustration for a device for testing the “force of the vacuum”, as shown below, which was un upside down open-ended (at the bottom) cylinder ABCD, fitted with a piston head EFGH, through which the rod I, which could be raised or lowered (to let out air in the working body volume), was affixed to a bucket, into which weight could be added, so to see how much force was required to “break” the vacuum, as Galileo conceptualized things:
In 1654, Otto Guericke, amid his numerous vacuum experiments (see: Schott diagrams), carried out from 1647 into the 1670s, in part inspired by a reading of Galileo's Two New Worlds, and seeing his theoretical vacuum testing device, built what can be considered the modern variant of the piston and cylinder, and made a vacuum pump and vacuum bulb (bulb shown below being held by boy), to test the strength of the vacuum:
In 1670s, two different vertical or upright gunpowder engine, similar to da Vinci’s design, albeit with a rope and pulley mechanism affixed to lift the weight were invented by Christiaan Huygens (1673), below left and center, and his assistant Denis Papin (1674), followed, independently, supposedly, by Jean Hautefeuille (1678), one version of which is shown below right, albeit without the piston:
In 1690, Denis Papin, in his "A New Method to Obtain Very Great Motive Powers at Small Cost", detailed an outline for a theoretical engine (see: Papin engine) wherein water (working body) contained inside of a sealed piston and cylinder, namely cylinder A into which an adjustable piston B is inserted, the height of which, indicated by indicator M, being regulated by adjustable arm E, as shown below, is alternatively made to expand, via external fire (hot body) put in contact with the outside of the cylinder, and made to contract, via external cool water (cold body) contact to the outside of the cylinder, such to work the piston rod up and down. 
In 1765, there were Newcomen engines with 72-inch diameter cylinders, that were 10-feet in length. 
In 1824, Sadi Carnot devised the theoretical "Carnot engine", with the piston head level shown by line CD, encompassing working body volume of ABCD, which is generally based on the Papin engine, meant to encompass the overall principles of the general heat engine or all steam engines before him:
In 1849, William Thomson began to investigate Carnot's theoretical heat engine ideas, which began to launch thermodynamics as a new science.
In 1963, Joseph Needham, in his the ‘Pre-Natal History of the Steam-Engine’, wherein, according a review of Ladislao Reti (1969), he gives the supposed origin of every single part of the compound mechanism of the steam engine is discussed and traced, such as: boiler, cylinder, piston, double acting principle, crank, and rod transmission, etc. 
The following are related quotes:
“In about the year 1710, Thomas Newcomen, ironmonger, and John Calley, glazier, of Dartmouth in the county of Southampton (Anabaptists) made then several experiments in private, and having brought it to work with a piston, etc., in the latter end of the year 1711, made proposals to draw the water at Griff in Warwickshire; but their invention meeting not with reception, in March following, through the acquaintance of Mr. Potter of Bromsgrove in Worcestershire, they bargained to draw water for Mr. Back of Woolverhampton, where, after a great many laborious attempts, they did make the engine work; but not being either philosophers to understand the reasons, or mathematicians enough, to calculate the powers, and to proportion the parts, very luckily by accident found what they sought for. They were at a loss about the pumps, but being so near Birmingham, and having the assistance of so many admirable and ingenious workmen, they soon came to the method of making the pump valves, clacks, and buckets; whereas they had but an imperfect notion of them before. One thing is very remarkable; as they at first were working, they were surprised to see the engine go several strokes, and very quick together, when after a search they found a hole in the piston, which let the cold water into condense the steam in the inside of the cylinder, whereas before they had always done it on the outside. They used before to work with a buoy in the cylinder enclosed in a pipe, which buoy rose when the steam was strong, and opened the injection, and made a stroke; thereby they were capable of only giving six, eight, or ten strokes in a minute, until a boy, Humphry Potter, who attended the engine, added (what he called scoggan) a catch that the beam Q always opened: and then it would go 15 or 16 strokes in a minute. But this being perplexed with catches and strings, Mr. Henry Beighton, in an engine he had built at Newcastle on Tyne in 1718, took them all away, the beam itself simply supplying all much better. N.B. about the Tear 1717, I communicated to Mr. Beighton, the use of the steel-yard over the puppet-clack, or safety valve, which be applied to some engines. The way of leathering the piston was found by accident in about 1713: having then screwed a large broad piece of leather to the piston, which turned up the sides of the cylinder two or three inches; in working it wore through, and cut that piece from the other, which falling flat on the piston, wrought with its edge to the cylinder, and having been in a long time, was worn very narrow; which being taken out, they had the happy discovery, whereby they found that a bridle rein, or even a soft thick piece of rope or match going round would make the piston air and water-tight.”— John Desaguliers (c.1740), A Course in Experimental Philosophy, Volume Two (pgs. 532-33)
“Hautefeuille, if we may judge from evidence at hand, was the first to propose the use of a piston in a heat engine, and his gunpowder engine seems to have been the first machine which would be called a heat-engine by the modern mechanic.”— Robert Thurston (1878), A History of the Growth of the Steam Engine 
“The cannon [he wrote] was not only important in itself as a power-machine applied to warfare; it is a one-cylinder internal-combustion engine, and all of our more modem motors of this type are descended from it. The first effort to substitute a piston for a cannon-ball, that of Leonardo da Vinci, used gunpowder for fuel, as did Samuel Morland's patent of 1661, Huygens' experimental piston-engine of 1673, and a Parisian air-pump of 1674. Indeed, the conscious derivation of such devices from the cannon continued to handicap the development [of internal-combustion engines] until the nineteenth century, when liquid fuels were substituted for powdered.”— Lynn White (1962), Publication; cited by Joseph Needham (1987) in Science and Civilization in China: Volume 5 (pg. 547) 
“The only detail that differentiates Leonardo’s machine from those of Huygens and Papin is the system adopted for raising the weight sustained by the piston. Huygens and Papin use a pulley and rope transmission (the same that Leonardo adopted in his experiments with steam), while Leonardo preferred to attach a rod to the bottom of the piston.”— Ladislao Reti (1969), “Leonardo da Vinci the Technologist: the Problem of Prime Movers” 
1. Thurston, Robert. (1878). A History of the Growth of the Steam-Engine (pgs. 24-25). Appleton and Company.
2. Kirby, Richard; Withington, Sidney; Darling, Arthur; and Kilgour, Frederick. (1956). Engineering in History (pg. 154; 72-inch cylinders, pg. 165). Courier, 1990.
3. Reti, Ladislao. (1969). “Leonardo da Vinci the Technologist: the Problem of Prime Movers”, in: Leonardo’s Legacy: and International Symposium (editor: C.D. O’Malley) §4:67-100; quotes, pgs. 95-98). University of California Press.
4. (a) Needham, Joseph. (1963). “Pre-Natal History of the Steam-Engine” (abs)(Ѻ), Transactions of the Newcomen Society, 35:3-58.
(b) Reti, Ladislao. (1969). “Leonardo da Vinci the Technologist: the Problem of Prime Movers”, in: Leonardo’s Legacy: and International Symposium (editor: C.D. O’Malley) §4:67-100; quotes, pgs. 95-98). University of California Press.
5. Needham, Joseph. (1987). Science and Civilization in China: Volume 5, Chemistry and Chemical Technology, Part 7, Military Technology: The Gunpowder Epic (pg. 563). Cambridge.
● Piston – Wikipedia.