Azeri trains fill up but are not especially crowded except in student holidays. It's usually quite easy to get a ticket for the same day of travel as little as an hour or so before departure. However, it's worth buying early to get a well-positioned berth.
Georgian trains are so rarely full that tickets hardly ever go on sale until a couple of hours before departure. You will usually be asked to show your passport when buying a ticket.
From a station that is not the train's departure point there is only a limited quota of tickets available. Once this has been reached further bookings are impossible until about half an hour before the train's arrival or whenever it has signalled ahead the number of berths remaining. This explains why you might occasionally see a small queue at the ticket office that never seems to diminish.
When you have the ticket in your hand it's worth checking that the details are what you expected. On the most common computerized ticket there are four lines of print-out: the first gives the train number, departure date/time (getma tarixi), carrage number (vaqon) with a code for class (KP = kupe, PL = Platskart), base price, and your passport number (bring ID'). Hidden away on the second line between the routing and your name is the all-important berth number (yer). The third line lists any surcharges - including 'yataq' fee for bed-sheets. Ignore the last line which simply records the cashier and date of purchase. Ticket vendors are usually most obliging and will often underline the key points without being asked.
On boarding the train the provodnik initially examines the ticket to make sure you have the correct carriage and returns it. Once the train has departed he walks up the carriage collecting tickets in a numbered wallet. Being left ticketless is unnerving for first-time passengers but is quite normal. Still, for your own peace of mind, you can make a photocopy/camera shot of the ticket before boarding just in case there's a question later about the ticket getting lost' - especially on the Baku-Tbilisi train.
Azerbaijan railways - online booking available now
From 20 November 2011 Azerbaijan Railways started selling railway tickets online. One can book tickets both to local and international destinations. Booking is available through company's website at the following address http://railway.gov.az/en/login . Online orders could be placed on a daily basis from 07:00 to 23:00. No more than 4 tickets could be purchased in one transaction. Card payments are available. Traveler who placed and paid for the order online could collect tickets in the Tickets Office after showing his ID or printed order confirmation within 10 days and no later than 1 hour before the travel time. Online orders could be placed no later than 2 hours before the train's departure time from initial point.
Travel agencies won't be able to assist you with purchasing train tickets as for international directons they are sold only when you produce original passport of the traveler (photocopies not accepted). But generally places are always available, so that should not be your point of concern.They accept cash only!