Defeating them.
Source- this Bulbapedia page only mentions defeating the Pokemon not winning the battle. (if this source isnt enough, let me know and ill hide)
it gains evs from fainting the Pokemon. so if you run into a zangoose and seviper horde you can just kill the zangoose for the attack evs and flee the seviper, unless you want the special attack ev too.