By coincidence! And by a malfunctioning self-sinking system aboard U-110.

I think you'll find that it's very often llittle mishaps like this that determine the outcome of a battle, if not the whole war.
From what I can make out (never having seen the film) it seems to be based on a combination of 3 separate events. One was the surrender of U-570 to a Hudson of RAF Coastal Command off Iceland in August 1941. This was the first & I believe only time a submarine had surrendered to an aircraft & seems to be the origin of the U-571 in the title. The real U-571 was sunk by depth charges from an Australian Sunderland aircraft on January 28, 1944 - months before the U.S. Navy captured U-505. All hands were lost.
The action everybody say's it's based on involving HMS Bulldog & U-110 took place in May 1941
http://uboat.net/boats/u110.htm An Enigma machine was indeed captured but according to the historians at Bletchley Park the most important to them was the capture of the code books for a new type of Enigma machine, by HMS Petard from U-559 off Port Said on 1st February 1942.
http://www.ellsbury.com/ultrafassongrazier.htmFeelings were so strong in Britain about this film that PM Blair made a statement about it in Parliament.
http://news.bbc.co.uk/1/hi/uk/781858.stm