Abu Dharr reported:

I entered the mosque and the Messenger of Allah (ﷺ) was sitting there. When the sun disappeared (from the sight) he said: O Abu Dharr! Do you know where it goes? He (the narrator) said: Allah and His Apostle know best. He (the Holy Prophet) said. Verily it goes and begs permission, for prostration (to Allah) and the permission is granted to it. Once it would be said: Return to the place whence you came, and then it would rise from its setting place. Then he, after the recitation of 'Abdullah recited it: And that is its appointed term.
وَحَدَّثَنَا أَبُو بَكْرِ بْنُ أَبِي شَيْبَةَ، وَأَبُو كُرَيْبٍ - وَاللَّفْظُ لأَبِي كُرَيْبٍ - قَالاَ حَدَّثَنَا أَبُو مُعَاوِيَةَ، حَدَّثَنَا الأَعْمَشُ، عَنْ إِبْرَاهِيمَ التَّيْمِيِّ، عَنْ أَبِيهِ، عَنْ أَبِي ذَرٍّ، قَالَ دَخَلْتُ الْمَسْجِدَ وَرَسُولُ اللَّهِ صلى الله عليه وسلم جَالِسٌ فَلَمَّا غَابَتِ الشَّمْسُ قَالَ ‏"‏ يَا أَبَا ذَرٍّ هَلْ تَدْرِي أَيْنَ تَذْهَبُ هَذِهِ ‏"‏ ‏.‏ قَالَ قُلْتُ اللَّهُ وَرَسُولُهُ أَعْلَمُ ‏.‏ قَالَ ‏"‏ فَإِنَّهَا تَذْهَبُ فَتَسْتَأْذِنُ فِي السُّجُودِ فَيُؤْذَنُ لَهَا وَكَأَنَّهَا قَدْ قِيلَ لَهَا ارْجِعِي مِنْ حَيْثُ جِئْتِ فَتَطْلُعُ مِنْ مَغْرِبِهَا ‏"‏ ‏.‏ قَالَ ثُمَّ قَرَأَ فِي قِرَاءَةِ عَبْدِ اللَّهِ وَذَلِكَ مُسْتَقَرٌّ لَهَا ‏.‏
Reference : Sahih Muslim 159 c
In-book reference : Book 1, Hadith 308
USC-MSA web (English) reference : Book 1, Hadith 299
  (deprecated numbering scheme)