Short answer
Before. The sunnah is to break the fast as soon as the sun sets with a few dates or some water, then pray Maghrib, then eat your main meal. In the Hanafi school both hastening the iftar and praying Maghrib early are recommended, so keep the first bite light.
Hastening the iftar is mustahabb, and so is praying Maghrib early in its time. The way to do both is a light iftar of dates or water, then Maghrib, then the meal. Delaying Maghrib without an excuse until many stars appear is makruh.
The four schools agree on this.
Explanation
Anas ibn Malik said the Prophet ﷺ would break his fast with fresh dates before he prayed; if there were none, with dried dates; and if there were none, with a few sips of water. That is the model: something small at the moment of sunset, then the prayer.
Why not pray first? Hastening the iftar is the sunnah, and people remain upon good as long as they do it. Breaking the fast on a date takes a moment, and then you pray Maghrib with a settled mind.
Why not eat the whole meal first? Maghrib has a short time, and the Hanafi school dislikes delaying it until the stars fill the sky. A full meal before praying often pushes Maghrib late. That is why many masjids leave only a short gap between the adhan and the iqamah in Ramadan.
When food is already served. The Prophet ﷺ said that when dinner is served, start with it before praying Maghrib and do not rush away from it. The scholars explain this is for someone so hungry that he could not focus on the prayer. Eat what settles you, then pray within the time.
- At sunset: dates or water, and the iftar dua. See the dua for breaking the fast.
- Pray Maghrib, in congregation if you can.
- Then eat dinner.
The four schools agree that breaking the fast before Maghrib is the sunnah. For when exactly the fast ends, see when to break your fast.
The evidence
Quran, Surah Al-Baqara 2:187
أُحِلَّ لَكُمْ لَيْلَةَ ٱلصِّيَامِ ٱلرَّفَثُ إِلَىٰ نِسَآئِكُمْ ۚ هُنَّ لِبَاسٌۭ لَّكُمْ وَأَنتُمْ لِبَاسٌۭ لَّهُنَّ ۗ عَلِمَ ٱللَّهُ أَنَّكُمْ كُنتُمْ تَخْتَانُونَ أَنفُسَكُمْ فَتَابَ عَلَيْكُمْ وَعَفَا عَنكُمْ ۖ فَٱلْـَٰٔنَ بَٰشِرُوهُنَّ وَٱبْتَغُوا۟ مَا كَتَبَ ٱللَّهُ لَكُمْ ۚ وَكُلُوا۟ وَٱشْرَبُوا۟ حَتَّىٰ يَتَبَيَّنَ لَكُمُ ٱلْخَيْطُ ٱلْأَبْيَضُ مِنَ ٱلْخَيْطِ ٱلْأَسْوَدِ مِنَ ٱلْفَجْرِ ۖ ثُمَّ أَتِمُّوا۟ ٱلصِّيَامَ إِلَى ٱلَّيْلِ ۚ وَلَا تُبَٰشِرُوهُنَّ وَأَنتُمْ عَٰكِفُونَ فِى ٱلْمَسَٰجِدِ ۗ تِلْكَ حُدُودُ ٱللَّهِ فَلَا تَقْرَبُوهَا ۗ كَذَٰلِكَ يُبَيِّنُ ٱللَّهُ ءَايَٰتِهِۦ لِلنَّاسِ لَعَلَّهُمْ يَتَّقُونَ ﴿١٨٧﴾
It has been made permissible for you the night preceding fasting to go to your wives [for sexual relations]. They are clothing for you and you are clothing for them. Allah knows that you used to deceive yourselves, so He accepted your repentance and forgave you. So now, have relations with them and seek that which Allah has decreed for you. And eat and drink until the white thread of dawn becomes distinct to you from the black thread [of night]. Then complete the fast until the sunset. And do not have relations with them as long as you are staying for worship in the mosques. These are the limits [set by] Allah, so do not approach them. Thus does Allah make clear His ordinances to the people that they may become righteous.
The fast ends at sunset, which is also the start of Maghrib time.
Sunan Abi Dawud 2356
Grading to be checked... يفطر على رطبات قبل أن يصلي ...
"He would break his fast on fresh dates before he prayed."
The sunnah is to break the fast before praying Maghrib.
Sahih al-Bukhari 672
Sahih... إذا قدم العشاء فابدءوا به قبل أن تصلوا صلاة المغرب ...
"When dinner is served, start with it before you pray Maghrib."
Someone very hungry may eat first so that he can pray with focus.
Where the ruling is recorded
- Nur al-Idah with Maraqi al-Falah, Kitab al-Salah, chapter on prayer times. Praying Maghrib early is recommended, and delaying it until the stars are many is disliked.
- Nur al-Idah with Maraqi al-Falah, Kitab al-Sawm, chapter on what is recommended for the fasting person. Hastening the iftar is recommended.
Checked by our team. For a more accurate answer to your own situation, consult a scholar. Quran translation by Sahih International. Hadith Arabic from the standard editions of the six books and the Muwatta, with gradings as recorded there; the English is our own rendering. Spotted a mistake? Tell us.