The word serendipity has been around for more than two centuries, but only really began to be used much in the twentieth century, to the extent that the adjective serendipitous is not recorded before the 1950s.
Horace Walpole coined it in a letter he wrote to his long-time diplomat friend Horace Mann in 1754. He told him that he invented it in reference to the title of an old Persian fairy story "The Three Princes of Serendip" whose heroes regularly discovered pleasant things that they weren't searching for.
The story "The Three Princes of Serendip" tells of princes who were beloved of their father, and hence well-educated and intelligent men. They were sent out into the world by their father to test their mettle, and their adventures hence form the legend. Their good fortune (which was a bit slap-dash: they got thrown in jail as suspected camel thieves at one point) was based on careful deduction, not mere chance.
As the saying has it, "fortune favours the prepared mind", just as discoveries today that are said to be serendipitous are so often the result of experience and good observation. The three princes came from a country the Persians called Sarandib but which we now know as Sri Lanka, or in earlier times Ceylon. The Persian is a corruption of the Sanskrit Sinhaladvipa, “the island where lions dwell”, hence the name Sinhala or Sinhalese for the most commonly spoken Sri Lankan language.