It originated as a chapel for local people within the grounds of Barking Abbey, to the south of the Abbey church. Its oldest part is the chancel, built early in the 13th century during the reign of King John. The building is said to have been made into a parish church in 1300 by Anne de Vere, abbess of the Abbey. Until the 1390s Barking formed a rectory, held by the Abbey and divided into two vicarages known as 'Northstrete' and 'Southstrete'. The area suffered severe flooding in the late 14th century, leading to financial difficulties and a merger of the two vicarages from 1398 onwards. A chaplain from the Abbey led worship. The present bell tower was added late in the 15th century.
Reformation
It remained a parish church when the Abbey was dissolved and the rectory and advowson devolved to the Crown, who initially leased it to the widow Mary Blackenhall for 21 years in 1540. In 1557 these were bought by Robert Thomas and Andrew Salter using money from the estate of William Pownsett of nearby Loxford, and granted to All Souls College, Oxford, in return for the vicar praying for the souls of Pownsett, his parents and benefactors every Sunday, giving 6 shillings and 8 pence amongst twenty poor people annually on the anniversary of Pownsett's death, paying the College an annual sum to maintain two poor scholars and only being absent from the parish 80 or fewer days a year. The College presented when the next vacancy occurred in 1560, but at the following one the Crown contested its right, though this was overturned via a lawsuit. Sir John Petre reconfirmed the 1557 grant in 1594, but dropped the requirement to pray for the dead. The right is now shared between All Souls College, Oxford, the Bishop of Chelmsford and the church's churchwardens.
17th century to 20th century
The church contains several memorials, including one to the 17th-century politician Charles Montagu. The explorer James Cook married Elizabeth Batts in the church on 21 December 1762. Ten years later the nave, chancel and sanctuary all had their ceilings plastered, though this was removed from the nave ceiling in 1842. Charles Winmill and George Jack were involved in a restoration of the interior between 1929 and 1936. The building was Grade I listed in 1954. An extension was added along the south side late in the 20th century to provide an office, bookshop and refectory.
Present day
The church is now one of three in a team parish covering Barking – the other two are Christ Church and St Patrick's. The parish is unusual in having three churchwardens rather than the more normal two. In 2007, two small stones from remains of the old medieval London Bridge were joined together in a sculpture in front of St Margaret's church facing the Barking Abbey ruins as part of several public artworks placed in Barking Town Centre by artist Joost Van Santen.