My first worry would be the wiring harness. Check the archives for the variety of problems this causes.
It sounds like your ASR or EA/CC/ISC systems are what would be involved. Codes should be checked and be sure linkages are free and adjusted properly. The type of limp home function that does what you describe is protecting against unintended acceleration (throttle taking off on its own).
Had a S500 this week that did this; turned the ASR light on and the Batt light. We caught it in the act on actual values for DI and the screen I was looking at (I was monitoring timing basically as it only was setting a shift overload code and the speed was limited but surging) had timing and a number of other values including battery voltage. The timing was pulsing between 17 and 35 degrees and I happened to notice that the batt voltage was swinging from 14v to 17.7v. We tried a regulator but it still was full fielded. An alternator solved the problem. Twenty years ago I would have disassembled it and fixed it. Now the time is too valuable. (The car belonged to a tech in another shop who was doing the labor)
Bosch Master, ASE Master, L1
33 years MB technician