It was important for there to be a place where people could come and pray for the dead king. This was the purpose of the mortuary temple. In the case of the Old Kingdom pyramids, it was placed just inside the pyramid courtyard wall. (The wall no longer exists in the picture above.)
Not all mortuary temples were placed near to the tombs. In the New Kingdom, the mortuary temples of pharaohs were placed on the Nile floodplain at the entrance to the Valley of the Kings because there would not have been room in the valley. The two most spectacular of these temples are those built for Hatshepsut and Ramesses II.