Prayer was among the first rulings that Allah (Glorified is He) revealed. Prayer was set to be before sunrise and before sunset. This is due to Allah’s saying, {And celebrate the praises of your Lord in the evening and in the morning} [Surah Ghafir: 55].
When it was time to pray, the Prophet (peace be upon him) and his Companions used to go to the mountain passes, praying while hiding from their people. Abu Talib once saw the Prophet (peace be upon him) and Ali pray, so he discussed the matter with them. And when he learnt the prayer was obligatory, he told them to be steadfast in performing it [The Prophet’s Biography by Ibn Hesham].