Well, I'm sure there are several reasons, but purely on a power politics level, a fair bit of "the enemy of my enemy is my friend" seems at play here.

* Iran has had difficult relationship with Azerbaijan, despite (or maybe because of?) the Shia majority in the latter (as well). Depending which source you read, the locus of this animus varies, e.g. whether it [was](https://www.foreignaffairs.com/articles/united-states/2013-01-15/tangle-caucasus) the 'Greater Azerbaijan' rhetoric from the north side of the border, or the [fear](https://www.bbc.com/news/world-europe-19063885)  that the (rather secular) government had of Islamic-revolutionary subversion from the south. Maybe a bit of both.

* Allegedly, Iran sided with Armenia in the early days of the Nagorno-Karabakh conflict. (First source in previous para claims that, although I'm not privy to details.)

* Azerbaijan has an exclave in Nakhchivan. Although Iran could have offered a route bypassing Armenia, it looks like they didn't [until](https://eurasianet.org/azerbaijan-iran-sign-transport-deal-bypassing-armenia) 2022 or so, at least not a railway.

* Additionally, Azerbaijan needed weapons which Israel was willing to provide. (Their battlefield victories this decade relied on technological superiority of both Turkish and Israeli drones, on the most obvious footage level.) Although Israel appears to deny this, alleged surveillance of Iran from Azerbaijani bases (via drones at least) has been mentioned in the press. So that's allegedly some quid-pro-quo. (See previous sources.)