It likely happened in every battle. Several instances are reported at Gettysburg, on both sides. It was commonly the result of friendly artillery firing into their own infantry: either from a case of mistaken identity; or the infantry advancing into the existing fire zone; or faulty ammunition; or obscured conditions (such as darkness). Fellow infantrymen firing well behind the front line was another cause, but during a pitched battle it would difficult to pinpoint the culprit. Two examples:
USA: When shells from batteries in the rear began dropping into the 20th Connecticut at daybreak on July 3, taking both arms off Private George Warner, an angry Colonel William Wooster sent back word that if it continued he would turn his men around and charge them. Wooster omits mention of the incident in his official report, but his brigade commander, Col. A. L. McDougall writes that the regiment endured "an afflictive and discouraging, though accidental, fire of our own batteries."
CSA: General Stephen Ramseur mentioned an incident in his official report ... "exposed to the artillery of the enemy and our own short-range guns, by the careless use or imperfect ammunition of which I lost 7 men killed and wounded." As a result, Capt. Dance's artillery battalion, which had apparently inflicted the damage, was restricted to firing only the safer solid shot.
So far as is known, no one was held to account for these apparently unintentional acts.