If you want to travel from Iran to Azerbaijan, you have to use a bus. Buy your bus ticket locally in Iran.
If you want to travel from Iran to Turkey by train, buy your train ticket locally at a train station in Iran or check the given booking links.
There is a direct overnight train from Tehran (Iran) via Van (Turkey) to Ankara (Turkey).
This direct train connection between Iran to Turkey is currently cut down due to the political situation. We will update this information as soon as we have news.