In summary, D&D third edition (3.0 and 3.5) were the only editions where the druid automatically gains the Animal Companion class feature, and only in 3.5 does the ranger gain the Animal Companion class feature. In most other editions, various spells grant a similar ability to gain an animal follower, and after 3.5 there are various optional class features to gain the ability.
AD&D 1st edition
In AD&D, no classes had the Animal Companion class feature, but they did have a similar ability via spells.
The druid had a first-level spell called animal friendship, which permanently summons an animal, to a maximum total hit dice equal to twice the druid's level. Rangers gained first-level druid spells at level 8. Oriental Adventures featured a similar spell called animal companion for the shukenja and wu jen classes, which summons a friendly animal on a semi-permanent basis.
AD&D 2nd edition
In AD&D 2e, the ranger is is considered a subtype of warrior, and gains followers at 10th level, mainly human troops, but they can also include wild animals. The first-level spell animal friendship is still available, and can still be cast by druids from level 1 and rangers from level 8. The druid has no specific "animal companion" class ability other spells like this.
Player's Option: Skills and Powers features an Animal Master kit, which can be applied to an existing character and allows them to gain an Animal Companion. It's a Small size creature similar to a wizard's familiar, and doesn't become more powerful with level.
Priest's Spell Compendium Volume 1 gives a find companion spell, a fourth-level priest available to followers Malar in the Forgotten Realms, which conjures an animal companion which is specificall named as such. It's telepathically linked to the master, and gains an extra hit point per caster level.
You also have specific priesthoods who gain animal companions. Monster Mythology notes that clerics of the gnome deity Baervan Wildwanderer gain a raccoon animal companion.
D&D 3.0
In the initial release of D&D third edition, the druid gains an animal companion as an explicit class feature for the first time. They gain this ability from level 1.
The ranger does not gain animal companion as a class feature. They can still cast animal friendship to gain permanent animal companions. They gain this spell at level 4 at the earliest. Druids also have this spell from 1st level.
D&D 3.5
The druid continues to gain animal companion as a class level.
The ranger now gains Animal Companion as a class ability at 4th level.
D&D 4e
The ranger appears in the Player's Handbook. It does not gain Animal Companion as a class ability.
Martial Power introduces the Beastmaster ranger build which uses the Beast Mastery ability, gaining a "beast companion". It is an animal whose level increases with your own.
The druid appears in Player's Handbook 2. It also loses Animal Companion. However, animal friendship appears in that book as a ritual. It can only summon a tiny animal on a temporary basis (an week at most), and it can only perform a limited number of tasks like fetch an item, perform a trick, seek something, or watch an area.
The D&D Essentials product Heroes of the Forgotten Kingdoms introduces a new type of druid who gains an animal companion.
D&D 5th edition
In the Player's Handbook, the druid does not have animal companions.
The ranger, if they take the Beast Master, gains the Ranger's Companion feature.