There are several explanations for this, but no consensus. These include: 1) He often spoke Greek; 2) He did on this occasion; 3) agape and phileo are synonymous anyway : 4) the conversation was in Aramaic and two different Aramaic words for love (chav and racham) were used; and 5) the conversation was allegorical and represented Peter's dawning comprehension of his role.
Jesus and His disciples may, contrary to majority opinion, often have spoken in Greek.This article propounds that view, arguing that there are other examples of wordplay in the gospels which make sense only in Greek, and not in Aramaic. Most though, think Aramaic was the usual language Jesus spoke in.
Even if Aramaic was Jesus's usual language, this particular conversation shows evidence of extraordinary formality. Peter is addressed, not as Peter, but as Simon son of Jonas, and Jesus uses this "full" name not once but three times. This is very formal. Greek was a more official, or formal, language than Aramaic, so Jesus may have initiated the conversation in Greek to further emphasise its formality.
Many scholars no longer think there is, in fact, any significance in the difference between agape and phileo. It is not unusual to use synonyms for purely stylistic reasons. This article explores this question and includes the statement:
More evidence is also deduced from the silence of the Greek speaking Fathers of the Church who do not make mention of this distinction in the verbs for love when they comment on this passage. One would think that had the subtle distinctions been significant they would surely have dwelt upon it.
Neither the King James Version nor the Douay version pick up on the difference. The NIV previously translated agape as "truly love" and phileo simply as "love", but the 2011 edition loses the distinction, translating both identically. This reflects a trend in scholarship towards the earlier view that the words agape and phileo are virtually synonymous and interchangeable.
A more detailed exegesis arguing for this view, and briefly describing the rise and decline of the belief the difference is significant is here.
If the conversation was in Aramaic then the two different Aramaic words for love and friendship may have been used. These are Chav (also transliterated in English as hooba) and Racham (or raham). Here are more details on these Aramaic words for love, in St John's Gospel, though not in this particular context. If the evangelist knew of the conversation in Aramaic, and wished to convey it in Greek, he may have translated chav as agape and racham as phileo. In this way he represented the difference in Aramaic, by using different words in Greek. The exact nuances of agape versus phileo, and of chav versus racham, may not exactly correspond, and there is no real consensus on the nuances of any of them.
The conversation may never really have happened. That is the view of some who reject the Resurrection as a literal event. Bishop John Shelby Spong, in his book Resurrection, Myth or Reality (not free on the web, alas) supposes it could have been an hallucination brought on by Peter's guilt over the fact that, despite promising that whatever anybody else did, he would never abandon Jesus; had then done exactly that. Perhaps, Peter reflected, he had not, when it came down to it, demonstrated his love for Jesus any more than the others had. Perhaps he had not truly loved Him at all; but at any rate he was definitely His friend who cared passionately about Him, and there was great work for him still to do for Jesus. Peter had to look to the future. Jesus needed Peter, now more than ever, to shepherd his flock. On this view the question of what language the imaginary conversation took place in is not relevant.