Why are Mahayana texts in Sanskrit? 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.'
I'm afraid any answer to this is going to be speculative, so not a perfect SE question. Here is my theory:

Most of the early Mahayana texts were written either in Hybrid Sanskrit or in Gandhari Prakrit because their authors were the descendants of Aryans living in the Gandhara region. Historically, that area has always been much more "sanskritized" than the rest of India, see below for my explanation as to why.  So when Buddhism reached Gandhara, it was only natural for the educated locals to write their thoughts about Buddhism in a language that they both spoke and knew how to write - and that was Hybrid Gandhari Sanskrit.

Here is some background information in support of this thesis:


As you all know, Classical Sanskrit is an idealized written form of the language that the original Aryans actually spoke (some modern scientists call it "Prakrit" and other call it "Vedic Sanskrit"). Sort of like Classical Latin is an idealized written form of the language that the original Romans actually spoke.
Originally, Aryans came from Iran and settled in what today is south of Afghanistan, along what used to be Sarasvati river. Then, when the tectonic shifts in Himalayas made Sarasvati change its course and start falling into Ganges, the Aryan tribes started migrating further east, carrying their language and culture with them, and mixing with the indigenous population. This led to a range of dialects emerging along the Ganges, from the purest form of Vedic Sanskrit in the west to the most corrupted vernaculars in the east. This process was accompanied by social stratification, with "more civilized" Aryans on the top and "less civilized" locals at the bottom - this was how the infamous casts or varnas begun.

Sorry about the map labels being in Russian, I drew it a long time ago and don't have time at the moment to fix the labels. But from the overall shape and the colors, I hope you can see the state of Aryan assimilation by the time the Buddha comes on stage. The blue states on the left are more Aryan, speaking almost pure Sanskrit - and the orange states on the right are more the original people, speaking their own languages partially mixed with Sanskrit to form various dialects like e.g. Pali. The region of Gandhara is not highlighted on my map, but it is that area with multiple rivers  to the left of the leftmost Indian state.


There was a big university there in Gandhara, called Takshasila University - and thanks to the proximity of Persian and Greek Empires I suppose this region was a lot more educated and literate than the rest of India, back in the day.
Most of the Mahayana scripts written in Sanskrit (either Hybrid or Gandhari) were discovered at archaeological sites located in the Gandhara area.
Many of the great Mahayana lineages recognized in Tibetan Buddhism, trace their beginning to enlightened masters from Oddiyana which is basically the same area. 
Sarvastivada school left their version of Canon written down in Sanskrit, and we know that Sarvastivada was essentially centered in that same very region of Gandhara/Oddiyana.
Milinda-Panha (Questions of King Milinda) - a late Canonical text whose story is set in Sagala, a city just to the east of Gandhara region, was probably originally written in the Gandhari language.
At least some of the Mahayana sutras only available in Chinese show linguistical evidence of having been translated from the Gandhari dialect of Sanskrit.


All this is to say that ancient people of Gandhara/Oddiyana who wrote Buddhist texts in Sanskrit did not violate Buddha's rule prescribing people to "learn the word of the Buddhas each in his own dialect" - because Sanskrit was a written version of the language they actually spoke.

Then, from Gandhara, Buddhism has spread to other countries including China via the Silk Road, as well as Tibet.