The number seven is often used in the Bible to convey the idea of perfection. It may be that the same idea holds true in this case. The suffering of the Philistines had reached a limit and now the time came for the ark to return.1 This does not mean that the seven months are purely figurative, however. The Lord is sovereign; he has influenced events in such a way that it has taken seven months of tumours and plagues before the Philistines decided to send the ark back to Israel.
1 The ark of the LORD was in the country of the Philistines seven months.