England celebrate winning the first Test between Bangladesh and England at Zohur Ahmed Chowdhury Stadium on October 24, 2016 in Chittagong, Bangladesh. (Photo by Gareth Copley/Getty Images)

England has narrowly avoided an embarrassing defeat to Bangladesh in Chittagong, beating the hosts by 22 runs on the final morning of the Test.

Bangladesh began day five needing 33 runs to win, with just two wickets in hand, before Ben Stokes took the final two wickets of the match, to hand England victory.

Stokes removed both Taijul Islam and Shafiul Islam within three balls to deny Bangladesh their first ever Test victory against England.

No.7 batsman Sabbir Rahman led the Bangladesh charge, finishing on 64 not out, but it was not enough, as he ran out of partners at the other end.

The loss is Bangladesh's narrowest defeat in Test history.

Gareth Barry finished with figures of 3-65, whiel Moeen Ali, Ben Stokes and Stuart Broad all claimed two wickets each.

Earlier, England won the toss and elected to bat, scoring 293, thanks largely to Ali (68) and Johnny Bairstow (52), who rescued the innings, after England fell to 3-21 after the opening 12 overs.

Bangladesh then hit 248 in reply, before England scored 240 in their second innings, setting the hosts 286 runs for victory.

The second and final Test begins on Friday in Mirpur.