Based on the quote below, it seemed that the Buddha did not allow his teachings to be translated to the royal or priestly language of Sanskrit, that was the liturgical language of Brahmanism, the language of classical literature and the language used by the royal court.
The common people at the time did not speak Sanskrit in their daily conversations. They spoke various dialects of Prakrit. The Buddha wanted his teachings to be transmitted in the dialects of the common people.
It appears that Pali, although not a genuinely spoken language of the past, seems to be a mix of various Prakrit dialects from Buddha's time, that underwent partial Sanskritization.
It also makes sense to me that the Pali Canon was transmitted mostly by oral tradition and was written down only late in its history compared to Mahayana texts because Sanskrit was the main written language for a long time in northern India. This is similar to the role of Latin in European history.
I also guess that the Chinese scholar monks who visited India, took with them the Sanskrit Mahayana texts back to China, instead of the Pali Canon, partly because the Sanskrit texts were committed to writing much earlier, while the Pali Canon was still transmitted mostly orally at the time.
Question:
If the Buddha did not want his teachings to be taught in Sanskrit, why and how did the Indian Mahayana texts (agamas, sutras, vinaya etc.) end up being in Sanskrit?
According to the (Theravada) Vinaya from Cullavagga, fifth Khandaka, chapter 33:
And so sitting those Bhikkhus spake to the Blessed One thus:
'At the present time, Lord, Bhikkhus, differing in name, differing in lineage, differing in birth, differing in family, have gone forth (from the world). These corrupt the word of the Buddhas by (repeating it in) their own dialect. Let us, Lord, put the word of the Buddhas into (Sanskrit) verse.'
'How can you, O foolish ones, speak thus, saying, "Let us, Lord, put the word of the Buddhas into verse?" This will not conduce, O foolish ones, either to the conversion of the unconverted, or to the increase of the converted; but rather to those who have not been converted being not converted, and to the turning back of those who have been converted.'
And when the Blessed One had rebuked those Bhikkhus, and had delivered a religious discourse, he addressed the Bhikkhus, and said:
'You are not, O Bhikkhus, to put the word of the Buddhas into (Sanskrit) verse. Whosoever does so, shall be guilty of a dukkata. I allow you, O Bhikkhus, to learn the word of the Buddhas each in his own dialect.'