More than 28,000 prosecutions brought by two rail firms for alleged fare evasion have been quashed.