The Imams (a.s.) know when they will die and they die voluntarily

Al-Kāfi|Volume 1|Book 4|Chapter 47