Saying you "accidentally" did something is the only correct way. Saying you did something "on" accident or "by" accident is incorrect. The accident is not a person, place, or thing. You could neither have been on it nor by it when you did whatever you did.
Congratulations. You've just made me go full Grammar Nazi.
You don't sleep in a bed, either, you sleep on a bed. Are you going to try to tell me that the phrase, 'I'm in bed,' is grammatically incorrect?
And for what it's worth, 'by' doesn't have to indicate position. Your homework must be done by the due date. By can be an indicator of the cause of something, or to place responsibility for some event or action. 'Jane was hit by the truck,' might (incidentally) indicate the truck through position, but, 'Jane was put in the hospital by the truck,' indicates not that the hospital is adjacent to a truck, but that the truck which hit her was the cause of her hospitalization. Similarly, an accident can be that cause—so that morning, Jane put salt in her coffee by accident. An 'accident' was the cause of her putting salt in her coffee, as indicated by the word 'by' (and in this sentence, 'by' indicated that 'the word "by"' was the cause of the accident being indicated ).
Grammatik macht frei.
Yes I will. Your homework must be done "before" or "prior to" the due date. Both of your statements "Jane was hit by the truck" and "Jane was put in the hospital by the truck" are left with ambiguous meanings which could have been avoided had you used proper syntax. Did the truck strike Jane? Did someone or something else strike Jane adjacent to the truck? Did the truck put Jane in the hospital? Was Jane put in the hospital adjacent to the truck? The truck hit Jane. Jane was hospitalized as a result of the truck striking her.