According to our vedas it has been mentioned that a grahan occurs when rahu the head and ketu the body of the demon swarbhanu gallops sun and moon.
All of this began in the time when no human existed; the universe was a playground and battlefield of gods and demons.
Once it was decided the body of ocean must be churned which is full miraculous things even the liquid known as amrit which can make anyone most powerful and immortal.
The demons and the gods both wanted the amrit, so decided to help each other to churn the shir sagar only on a condition to share the nectar of immortality with each other.
But when the nectar appeared the gods realized that if the demons will get they will create havoc in the universe and no one will ever be able to stop them to come out of this dilemma the gods went to their deity the god of the gods lord Vishnu and he incarnated himself as the most beautiful creature ever.
Who spell bounded every demon and she started distributing the nectar disguised just as plane water to the demons but one asura saw through the treachery and sat among the gods and drank the nectar the amrit but moon god and sun god saw him and alerted Vishnu who beheaded the demon.
But as an effect of amrit he did not die but lives head as rahu anybody as ketu, over time to time they both take their revenge of being dismantled from surya and chandra by galloping them and make them suffer for there doing, this is why grahan occur be it chandra grahan or surya graham.