The most accurate response to the question of why Muhammad retreated to a cave outside of Mecca is:
He was looking for wisdom from God.
During his time in the cave, known as the Cave of Hira, Muhammad received his first revelations from the Angel Gabriel, which ultimately led to the establishment of Islam.