Mahasamādhi (the great and final samādhi) is the act of consciously and intentionally leaving one's body at the time of death.  A realized yogi (male) or yogini (female) who has attained the state of Nirvikalpa Samadhi (enlightenment), will, at an appropriate time, consciously exit from their body and cease to live. This is known as Mahasamadhi. This is not the same as the physical death that occurs for an unenlightened person.
Enlightened yogis take their Mahasamadhi during their final practice of samadhi: and they expire during this final sadhana practice. Therefore, Mahasamadhi occurs only once in a lifetime, when the yogi finally casts off their mortal frame and their karma is extinguished upon death.
An enlightened or realized yogi is one who has attained the nondual state of Nirvikalpa Samadhi where duality of subject and object are resolved and the yogi becomes permanently established in the unity of full enlightenment.
Each realized yogi enters and prepares for Mahasamadhi in a unique fashion. Blackman (1997) furnishes a number of examples.
- Blackman, Sushila (Compiled and edited) (1997). Graceful Exits: How Great Beings Die: Death Stories Of Tibetan, Hindu & Zen Masters. New York, USA: Weatherhill, Inc. ISBN 0 8348 0391 7