The captain can be changed at any time during any game, provided he/she is one of the playing eleven (not a substitute player). The law doesn't recognize "farewell matches", so that is immaterial. Dhoni could have made Mohammad Shami the captain if he wanted to.
3. Captain
- If at any time the captain is not available, a deputy shall act for him.
- At any time after the nomination of the players, only a nominated player can act as deputy in discharging the duties and responsibilities of the captain as stated in these Laws.