The first temples were built to protect statues of gods from the weather. They were not built for congregational worship. See information on sacred groves.
When temples became large and important structures it began to look as though the surrounding space was a garden, attached to a temple as domestic garden is attached to a house. This was especially so when temples were closed to public access and the space within the protective wall became a compound for priests.
Buddhist Temples also tend to have gardens because Buddha meditated in a park. They are found in India, China, Japan and elsewhere in South East Asia.
Japanese temple gardens