Here's a good pub quiz question. In which piece of music, apart from Messiah, did Handel include his famous Hallelujah Chorus? Answer: the Foundling Hospital Anthem. The composer frequently recycled sections of his own music: his coronation anthem Zadok the Priest appeared in his later oratorio Esther, his oratorio Israel in Egypt borrows from his funeral anthem for Queen Caroline, Messiah itself borrows from his early Italian duets. This was common practice – Handel didn't just borrow from his own work, he also borrowed from Telemann, Muffat, Bononcini and others, so much so that Handel's contemporary, the composer William Boyce, said of Handel: "He takes other men's pebbles, and polishes them into diamonds." . Perhaps another less well known part of Messiah's story and Handel's music is its intimate connection to the Foundling Hospital, and the composer's lifelong support of the UK's first children's charity, which continues today as the children's charity Coram.

Writing opera was a financially precarious business, then, as now. In 18th-century England, it was up to the composer to rent the theatre, hire the singers and musicians, and pay for costumes and scenery. Profits were hard to come by and the Italian operas Handel had been writing in the early years of his London career were falling out of fashion. In developing the English oratorio, the composer found a way to capitalise on the power of an orchestra, soloists and a choir of voices, while dispensing with the need for expensive sets, costumes and props.

However, oratorios, like operas, still required a large performing venue. Messiah premiered in Dublin in April 1742 in a charitable performance. For the London premiere the following year, Handel rented the Covent Garden Theatre from his friend John Rich for a week's run. Although a popular form of entertainment, the theatre was considered disreputable by most and even sacrilegious by some, and was deemed an inappropriate place to perform the sacred story of the Messiah. Indeed Handel was so concerned about objections that he suppressed the word "Messiah" in advertisements – the work was listed only as "A new sacred Oratorio". But even so it wasn't a success: the Earl of Shaftesbury noted in his memoirs that, "partly from the scruples some person had entertained against carrying on such a performance in a Play House, and partly for not entering into the genius of the composition, [Messiah] was but indifferently relish'd".

Had it not been for a newly established children's charity, Britain's best-loved and most famous choral work could have shared the fate of many of Handel's operas and faded into obscurity. For, without the Foundling Hospital, Messiah would have been silenced.

In 1742, on the northern edge of London, the foundation stone had been laid for the Foundling Hospital, a major new public building in what is now Bloomsbury. Following 17 years of campaigning, the philanthropist Thomas Coram had been granted a royal charter in 1739 by George II to establish a new charity to care for the babies that would otherwise have been abandoned on doorsteps and rubbish heaps by desperate mothers, usually young and unmarried, who had nowhere else to turn.

Unusually, one of its founding governors was an artist, William Hogarth. A committed supporter of the Foundling Hospital and a canny entrepreneur, Hogarth realised that if the charity was to succeed, it needed to establish itself in the public's imagination. He first designed the hospital's logo and then donated a full-length portrait of the charity's indomitable founder, his friend, Thomas Coram. This stunning painting can still be seen today in the Foundling Museum, on the site of the former Hospital. Having set the ball rolling, Hogarth then persuaded leading artists such as Joshua Reynolds, Allan Ramsay and Thomas Gainsborough to donate works. It was to be a win-win scheme. In the mid-18th century, there were no public art galleries. By effectively creating at the Hospital the UK's firstpublic art gallery, Hogarth gave the public a reason to visit and the hospital rapidly became one of the most fashionable charities in London. Once there, a visitor would see not only the best in contemporary British portraiture, landscape and maritime painting, they would also see the children at mealtime and singing in the chapel, and perhaps donate money.

Whether Handel was inspired by the astute creative philanthropy of his artist colleagues, we will never know. What is certain is that he recognised the chapel's potential as a performance venue that was free from the troublesome associations of the theatre. He was also probably aware that his royal patron was providing almost half of the funds to build the chapel. In May 1749, he approached the hospital's governors and offered to conduct a benefit concert. The programme included the first performance of Handel's "specially written" Foundling Hospital Anthem, which opens with a text adapted from Psalm 41 – "Blessed are they that consider the poor and needy … they deliver the poor that crieth, the fatherless", in a clear appeal to the audience to support the work of the hospital. The music was borrowed in part from his funeral anthem for Queen Caroline, his oratorio Susanna, and music by Lotti and Kuhnau, ending with the Hallelujah Chorus, lifted directly from Messiah – a work that few of his audience would have known. Also included on the bill was his Music for the Royal Fireworks, which had been premiered the month before, when its rehearsal at Vauxhall Gardens had caused a three-hour traffic jam of carriages, such was his popularity and people's desire to hear the music.

The benefit concert was a huge musical and financial success, and soon after Handel donated an organ to the new chapel. The following year he returned to the chapel to conduct a second benefit concert for the charity. This time he chose Messiah. The event was so oversubscribed – wealthy supporters had to be turned away on the night – that Handel was asked to repeat the concert two weeks later. To show its gratitude, the hospital made Handel a governor.

Thereafter, Messiah was performed each year in the Foundling Hospital chapel, for the benefit of the charity, a tradition that continued until the 1770s. Handel conducted or attended every performance until his death in 1759. These concerts not only helped secure the Oratorio's place in the nation's affections, they succeeded in raising the huge sum of £7,000 for the charity.

As a final act of generosity, Handel left in his will a fair copy of the Messiah score to the governors of the Foundling Hospital, thus enabling the charity to continue staging the benefit concerts – which they could not have done without the performing parts available for them to use. The score and parts were delivered to the hospital three weeks after Handel's death, and can be seen still today on display in the Foundling Museum, alongside his original will.

The creative philanthropy of Hogarth, Handel and their contemporaries was remarkable, but their support was not without professional self-interest. The two artists were pioneers in their respective fields and they needed platforms on which to promote their work. The Foundling Hospital provided the perfect place for fashionable society to enjoy contemporary art and music while supporting a very worthy cause, and the museum today celebrates the ways in which artists of all disciplines have been inspired to improve children's lives since 1740.

• Messiah at the Foundling Hospital will be broadcast on BBC2 in April.

• By George! Handel's Music for Royal Occasions is at the Foundling Museum, London until 18 May.

Caro Howell is director of the Foundling Museum.