Mahasamādhi (the great and final samādhi) is the act of consciously and intentionally leaving one's body. A realized yogi (male) or yogini (female) who has attained the state of nirvikalpa samadhi, will, at an appropriate time, consciously exit from their body. 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 (Videha mukti).
Each realized yogi enters and prepares for mahasamadhi in a unique fashion.