Austria: The first toilets were installed in baggage cars for train personnel in the 1850s. (they were not allowed to leave the train). The trains used to have rather long stops in stations in the first years, allowing passengers to get out, go to toilet, even have lunch.
The first toilets for passengers came up in 1869 on the Kaiser-Ferdinands-Nordbahn. At that era the coaches had no corridor connections so the toilets were only accessible to passengers in the adjacent compartments.
Around 1880 side corridors came up, from then on express train coaches were regularly built with toilets. However they only consisted of a toilet bowl with a hole in the ground and a water ewer for manual flushing.
Much later the toilets had a flap (so you didn't get a cold butt in winter) and water flushing operated with a foot pedal.
In the 1980s the first vacuum toilets were introduced, with a holding tank that has to be emptied regularly. Open toilets were no longer deemed acceptable because high speed trains must be airtight and new tunnel lines had to be kept clean.
The latest development is a bioreactor which separates water from the rest of the waste, allowing longer periods between emptying the tank. The separated water is used for flushing (don't worry, it will not come out of the tap).
The majority of Russian trains still have open toilets which are locked whenever the train is near a big city or crossing a border. (So you need to time your bladder well!)
Rabl/Stockklausner: "Österreichische Personenwaggons 1832 - 1982"