I had to take shelter from the rain under a tree.

Literal

Tree [possessive-の] under [at-で] rain-sheltering must-do.

~しなければならない in polite past form: 雨宿りしなければなりませんでした. Note that this is the polite past form of the obligation construction — quite long, but a regular stacking of ~しなければ+なりません+でした. A cumbersome but grammatical stack typical of polite Japanese.