It is generally understood that due to conditions in the environment, one must stray away from the african coast if one wants to reach the Cape of Good Hope. which was a longterm objective of the Portuguese Crown. But the discovery of the islands (or rather "Rediscovery", as they had been known previously), happened in the 1420's (technically Madeira was discovered in 1419, but it comes close enough to count).
If you look at a map or globe, the islands are nowhere south enough to be found by straying from the african coast, and, in fact, the straying from coast didn't start until a couple decades later (this was how Brazil was discovered, I'm told)
So what pushed the Portuguese to head West, when they were already starting to push towards Africa? Was it an attempt to find the islands? Were they going for any other place they hoped would be there?
The discovery of Madeiras and Azores happened before Portuguese started to really push towards Africa, and it was probably those discoveries that triggered the going to Africa. Also straying from the coast wasn't uncommon in the early 15th century
But their discoveries came in a more coincidental, rather then intentional, way.
Madeira was (re)discovered much after the Canaries were. In the early 15th century (1402) French in the service of Castillians colonized a Canary island and especially since then it was not uncommon that ships, including Portuguese, visited Canaries to supply the colonists or to trade with or just raid the native population.
While we don't know the exact motivation of the voyage of Portuguese who claimed to discovered Madeira we know Portuguese knew about and visited Canaries, and it is highly probable that sooner or later some ship going to Canaries would have spotted Madeira, given that Madeira is not that far from Canaries (especially with navigation back then)
Alternative explanation is similar: since 1415 and conquest of Ceuta (opposite Gibraltar) Portuguese started sailing more and more down Morocco, raiding and corsairing, when eventually they spotted Madeira. Both version include conventional sailing and accidental spotting.
Discovering Azores was not much different. It was a byproduct of the return voyage based on following the current and wind patterns in that part of Atlantic. Portuguese found that to return to Lisbon from Madeira, Canaries, Africa etc. by going direct route north you will go against the prevailing winds and current which might significantly prolong your journey. But if you went North, North West, you will reach latitudes to catch westerlies that will catapult you directly to Lisbon and mainland Portugal in same or less time than to take the direct "shorter" route. And it just happens to be that Azores lay on exactly those latitudes. And so they eventually stumbled on Azores. Map to show you this patterns exactly
However, this all does not mean it was all entirely by chance or that nobody expected to find anything. The Portuguese did actively look for land or islands all the time. The medieval books and early maps at the time had many correct but even more incorrect information about the Atlantic, and plenty of them showed unvisited speculative islands named Antilles, Island of 7-cities, island of "Brazil", etc which in their descriptions were rich beyond measure. (Not to make confusion, names of this mythical places were later used for real places, but that does not mean those islands were known at the time)
Infante Henrique (Henry the Navigator) was particularly interested in discoveries of those island or discoveries of any land, and sponsored ships and enticed crews to search and report new islands or lands. He was also for a long time working hard to annex Canaries from Castille to Portugal (or just to himself), and to support the Ceuta project by raiding down Morocco's coast, and searching for the source of trans-saharan trade and gold, and to look for Prester John, a mythical christian empire that would help them crush Moors. Given his support and pushing, it is not strange that Portuguese ships (increasing in numbers and trips) eventually found the islands