Firefighters work to free the horse. Photo: San Bernadino County Fire Dept/Twitter

A horse trapped upside-down in a ditch has been rescued by firefighters in California.

The rescue was carried out by crews of the San Bernadino County Fire Department and the Rancho Cucamonga Fire Department late on Tuesday.

It is understood the horse and its rider were trying to make progress up a ravine when the horse tumbled, and ended up in the four-foot deep ditch near the corner of Elm Street and Alta Mesa Road in Hesperia.

Firefighters had to use specialized equipment in the night-time rescue. In all, more than 20 firefighters attended the scene, including a special animal rescue team from the Rancho Cucamonga Fire Department.

A veterinarian was called to tranquilize the horse.

San Bernadino firefighters tweeted details regularly as the rescue proceeded.

Following the rescue, the horse was able to walk unassisted to a trailer and left the scene.