Apple, whose streaming service has rapidly grown but still has only half the paid subscribers of Spotify, said that Shazam has consistently been one of the most popular items on its App Store.
"We have exciting plans in store, and we look forward to combining with Shazam upon approval of today's agreement," it added.
Meanwhile, London-based Shazam said in a separate statement: "We can't imagine a better home for Shazam to enable us to continue innovating and delivering magic for our users."
The two companies did not disclose financial terms.
Shazam, which was founded in 1999 in the early age of online music, has offered a solution to a longtime agony of listeners -- putting a name to elusive songs. With a click, the app identifies tracks playing on the radio, at parties or as background music.
But Shazam has struggled to find a way to make money off its technology, even as it said that it had reached one billion downloads on smartphones last year.
Apple, which revolutionized online music a generation earlier with iTunes, in 2015 launched Apple Music as the market turns to streaming, which offers unlimited on-demand listening.
Apple said in September that the service had more than 30 million subscribers -- a quick rise, but still trailing Spotify.
Spotify said it had 60 million paying users as of July and 80 million more on its free tier.