This is a problem I encountered with my 240D, and I had to have the ring gear replaced. The reason you only get a failure every so often is the crank stops near the top of one of the pistons' compression strokes. Multiple pistons gives you multiple places to stop on the circumference of the ring gear. Consequently, if the starter engages an ok spot, the engine starts. If it engages a worn spot, you can get a nasty metallic clank/grinding sound, and the engine won't start. The only answer in the case of a bum ring gear is a new one. If the pinion gear on the starter is worn, you need a new one of them too or the starter won't work for long - the same mechanism that wrecked the ring gear originally will wreck the next one.
Hope yours is not the ring gear, as that is a greater pain to replace than the starter bendix. Jim
1986 Euro 190E 2.3-16 (291,000 miles),
1998 E300D TurboDiesel, 231,000 miles -purchased with 45,000,
1988 300E 5-speed 252,000 miles,
1983 240D 4-speed, purchased w/136,000, now with 222,000 miles.
2009 ML320CDI Bluetec, 89,000 miles
1971 220D (250,000 miles plus, sold to father-in-law),
1975 240D (245,000 miles - died of body rot),
1991 350SD (176,560 miles, weakest Benz I have owned),
1999 C230 Sport (45,400 miles),
1982 240D (321,000 miles, put to sleep)