Britain withdrew from Egypt in stages over a number of years. Each stage of withdrawal had a different cause.
Britain formally declared Egypt to be independent in 1922. It did so because of rising nationalist sentiment in Egypt at the time. Egypt had seen the Ottoman Empire breaking up after WWI due to nationalism and it felt this same urge towards nationalism. The British did not want to have to deal with this and officially made Egypt independent.
However, Britain did retain a fair amount of influence in Egypt. This lasted until after WWII. After WWII, Britain gave independence to India. This reduced the incentive for Britain to protect the Suez Canal. However, Britain then started to feel the need to protect Israel and therefore maintained some influence in Egypt. This ended with the revolution of 1952 in which Nasser overthrew the king.
Britain finally left Egypt for good in 1956. This time, it did so because of the United States. The US strongly disapproved of Britain's role in the Suez War of 1956. Under pressure from the US, Britain completely withdrew from Egypt.