Praise be to Allah.
Allah, may He be glorified and exalted, said to His Prophet (blessings and peace of Allah be upon him):
“So stand firm and straight (on the religion of Islamic Monotheism) as you are commanded, you and those (your companions) who turn in repentance (unto Allah) with you, and transgress not (Allah’s legal limits). Verily, He is All-Seer of what you do”
Ibn Jareer (may Allah have mercy on him) said:
Here Allah, may He be exalted, says to His Prophet Muhammad (blessings and peace of Allah be upon him): stand firm and straight as your Lord has commanded, you and those who follow you in that, and call upon Him, as your Lord has commanded, and those who return with you to obedience to Allah and act in accordance with what their Lord has commanded, after they had disbelieved; and do not transgress against His command and do that which He has prohibited to you, for your Lord has knowledge of all the deeds that you do – both deeds of obedience and disobedience – and nothing of that is hidden from Him, for He sees all of it. So fear Allah, O people, lest your Lord look upon you when you are doing something contrary to His command, for He knows all that you do, and He is constantly watching you.
End quote from Tafseer at-Tabari (15/499)
Ibn Katheer (may Allah have mercy on him) said:
Here Allah, may He be exalted, instructs His Messenger and His believing slaves to be steadfast and persist in righteousness, for that is one of the greatest means of help towards victory over one’s enemies and going against one’s opponents. And He forbids transgression, which is wrongdoing, for that is a fatal error even if it is against a mushrik. And remember that Allah, may He be exalted, sees all that people do, and He is not unaware of anything, and nothing is hidden from Him.
End quote from Tafseer Ibn Katheer (4/354)
This came after Allah, may He be glorified and exalted, had mentioned the situation of those who are blessed and those who are doomed, and after He, may He be exalted, had said, addressing His Prophet (blessings and peace of Allah be upon him):
“So be not in doubt (O Muhammad (blessings and peace of Allah be upon him)) as to what these (pagans and polytheists) men worship. They worship nothing but what their fathers worshipped before (them). And verily, We shall repay them in full their portion without diminution.
Indeed, We gave the Book to Moosa (Moses), but differences arose therein, and had it not been for a Word that had gone forth before from your Lord, the case would have been judged between them, and indeed they are in grave doubt concerning it (this Qur’an).
And verily, to each of them your Lord will repay their works in full. Surely, He is All-Aware of what they do”
Having spoken of the situation of both parties, and mentioned the disbelief of those who worshipped idols alongside Allah, and that those who differed concerning the Book became divided into factions, Allah then instructed His Prophet Muhammad (blessings and peace of Allah be upon him), and the believers who were with him, to stand firm and straight (on the religion of Islamic Monotheism) as they were commanded, and follow the laws that Allah had prescribed, and to accept the sound beliefs of which Allah had told them, and not to deviate from that to the right or to the left, and to persist in that, and not to transgress by overstepping the limits of righteousness that Allah had set for them.
End quote from Tafseer as-Sa‘di (p. 390)
We did not find any particular story or incident because of which this verse was revealed. But the context of these verses indicates that the meaning of the verse has to do with what comes before it of the description of those who are blessed and those who are doomed, and the state of the mushrikeen and those who differed concerning the Book. So it is an instruction to the Prophet (blessings and peace of Allah be upon him) and those who believed with him, telling them to stand firm and straight, and not to deviate or overstep the mark. Hence it begins with the word fa (translated here as “so”), in the phrase “So stand firm and straight”, which connects it to what comes before it.
To sum up the meaning: You – O noble Messenger – know the situation of those who are blessed and the situation of those who are doomed, and We have told you of the situation of the mushrikeen and the situation of those who differed concerning the Book, and We have told you that every accountable person will be requited in full for his deeds. So adhere, you and the believers with you, to the path of adherence to the truth, and persist in that as Allah has commanded you, without being heedless or going to excess, and beware of overstepping the limits of moderation in all your words and deeds; do not be like those who became divided and differed after clear proof had come to them, so that you may be among those who are blessed, and not be among those who are doomed.
Therefore, when a man came to the Prophet (blessings and peace of Allah be upon him) and said: O Messenger of Allah, tell me of something in Islam that I shall not ask anyone else about after you. He said: “Say, ‘I believe in Allah,’ then stand firm and straight.”
Narrated by Ahmad (14991) and at-Tirmidhi (2410); classed as saheeh by al-Albaani in Saheeh at-Tirmidhi
And Allah knows best.