Why was Surya killed by Lord Shiva?
The story of Shiva (temporarily) killing Surya the sun god is told in the Ganesha Khanda of the Brahma Vaivarta Purana. (See page 349 here.) As I discuss in this answer, one of the most prominent incarnations of Vishnu in ancient times was the sage Narayana, son of Yama god of death and twin brother of the sage Nara. (Nara and Narayana were the prevous births of Arjuna and Krishna.) In any case, in the Brahma Vaivarta Purana's account, the sage Narada asks Narayana about the birth of Ganesha, and then he asks how the remover of obstacles could have suffered such a big obstacle as losing his own head:
If Ganesha is Vighnahara or Vighnesha, that is, the one who removes all impediments, how came he to incur an impediment? Why did he have his head chopped off?
Narayana responds that it was due to a curse that Shiva had incurred. Once Surya the sun god tried to kill the evil Rakshasas (demons) Mali and Sumali. But these Rakshasas were devotees of Shiva, so Shiva came to their rescue, piercing Surya with his trident and (temporarily) killing him. The Sun immediately went dark, and the sage Kashyapa, Surya's father, was furious. (Kashyapa is the son of Marichi, one of the mind-born sons of Brahma, and Surya is one of the Adityas, the sons of Kashyapa and his wife Aditi.) Shiva restored Surya's life to pacify Kashyapa, but Kashyapa still put a curse on Shiva that he would kill his own son just as he had killed Kashyapa's son:
Mali and Sumali were two demons who were devoted to Shiva. The sun-god Surya did not like the demons and was about to kill them. The demons prayed to Shiva and Shiva intervened. He struck Surya with a trident and this made the entire world plunge into darkness. Surya was the sage Kashyapa's son and so, for this act, Kashyapa cursed Shiva. "Just as you pierced my son's chest with a trident, your son's head will be chopped off one day," said Kashyapa.
By the way, as I discuss in this answer, the Rakshasa Sumali had a daughter named Kaikeshi, who married the sage Vishravas. Together they had several children, including Ravana, Kumbakarna, and Vibhishana. So it's possible that if Shiva had allowed Surya to kill Sumali, Ravana might never have been born!
One note of caution: references to Ganesha in ancient Hindu scripture are few and far between, since Ganesha only rose to popularity later on, so there is a possibility that this may be an interpolation in the Brahma Vaivarta Purana. (I raise a similar issue in my answer here concerning a passage in the Shiva Purana.)
Note: “The question: Why was Surya killed by Lord Shiva?” is licensed by Stack Exchange Inc (https://hinduism.stackexchange.com/); user contributions licensed under CC BY-SA.