This is actually a little tricky to answer, because some Pokemon, like Greninja, are technically released to the wild. Regardless, I'll account for those, and the following lists will assume each Pokemon is fully evolved, not using other gimmicks (holding a Z-crystal or having the Gigantamax factor), and Ash played Legends ZA, spoke to Korrina, or dialed Jirachi to get all the Mega stones. Pokemon with more than one mega still count as one.
Counting just the Pokemon On hand/In Rotation/at Oak's Lab — 17.
- Pikachu (Mega Raichu X/Y)
- Pidgeot (Mega Pidgeot)
- Bulbasaur (Mega Venusaur)
- Charizard (Mega Charizard X/Y)
- Heracross (Mega Heracross)
- Bayleef (Mega Meganium)
- Totodile (Mega Feraligatr)
- Sceptile (Mega Sceptile)
- Glalie (Mega Glalie)
- Staraptor (Mega Staraptor)
- Gible (Mega Garchomp/Garchomp Z)
- Pignite (Mega Emboar)
- Scraggy (Mega Scrafty)
- Hawlucha (Mega Hawlucha)
- Dragonite (Mega Dragonite)
- Gengar (Mega Gengar)
- Lucario (Mega Lucario/Lucario Z)
Squirtle is in training, and we have seen Ash come in contact with Squirtle again — 18.
- Squirtle (Mega Blastoise)
This is where the list gets a bit muddy. For Ash's released Pokemon, we have seen Ash meet Greninja again, but not Larvitar. I personally wouldn't count it, since it was never formally caught, but it's up to you if you want to — 19 (20).
- Greninja (Mega Greninja)
- Larvitar (Mega Tyranitar)
If we really want to bs ourselves into giving Ash more Megas, Ash did catch a Beedrill that he gave away to Casey and he did travel with a Latias for a bit, even though he never caught it. I personally won't stretch the rules for Latias, but if you want to give yourself Larvitar, why not Latias? — (22).
- Beedrill (Mega Beedrill)
- Latias (Mega Latias)
And this is me being generous and not counting other random Pokemon like that one Venipede he befriended or the Mega Latis and Rayquaza he commanded in the Hoopa movie.
TLDR: Ash would be able to Mega evolve 19 Pokemon, but depending on how much you want to extend the rules on which ones he might go back for/get back from others/catch that he traveled with, it can be capped at 22 unless you really want to go higher.
Souce.
Hope I helped!