All perfect praise be to Allah
As this is a tafseer related question so this does not necessarily require references, but it requires to make it clear how these 2 ayahs are compatible. I will try to explain this, with help of of tafseer from Dr. Israr Ahmed (late), which you can listen(in urdu) here.
There are many opinions about this, we will discuss 2 of them.
But first, let's understand the background of this. We don't need to discuss details here, just focus on these 2 ayahs, in first ayah, Allah is describing what munafiqeen used to say:
But if good comes to them, they say, "This is from Allah "; and if
evil befalls them, they say, "This is from you."
"You" here refers to Prophet Muhammad ﷺ, so Allah is describing that when something good happens, the munafiqeen say it is from Allah, Allah is great alhamdulillah and all that, but when something bad happens(these ayahs are related to a gazwa/war, so you can expect anything in war) they say that this is happening because of you(Muhammad ﷺ), because of your decision or with anything they can hold on to blame him.
So, allah nullify their claim in first ayah about this, and makes it crystal clear and whatever happens good or bad happens as per Allah's wish. So, you can't blame prophet for bad things and praise Allah for good things. You should have complete faith on Allah and his decisions.
Having said that, in the very next ayah, comes the different explanations. The most popular one is:
The first ayah is related to munafiqeen and to nullify what they say, although everything good or bad "IS" from Allah, but true muslims don't attribute anything bad to Allah, so allah is commanding "MUSLIMS" to attribute good things to Allah, and on bad things, a true believer should say that this is because of some of my mistake, and Allah wanted to warn me or something, and this bad thing is because of me not because Allah ENFORCED it on me. This is first view.
You can read more about why believers should not attribute bad things to Allah on internet.
Now, coming to 2nd view, which is that the reality is that everything good or bad is with the will of Allah,(Now, i will have to apologize for my weak english vocabulary, i don't know they word, so i will try to explain the scenario) so in second ayah, Allah is kind of repeating like if someone says something illogical and the other person answers him with a question in his logic. Like... for example, let's suppose, if i say something like "Hey, you look great in this dress, but this color does not suits you." And you will reply like "Oh, i look great in this dress, and this color does not suits me??(How come? how illogical...)" This is something like that in this ayah, Allah is saying that whatever comes good is from me, and whatever bad is from you(This is what munafiqeen say, how illogical is this?). This is the second view on this ayah.
1st view is more strong, and more scholars support that view(According to Dr Israr, and he also prefers this view.).
Now, let me quote some related things:
“No evil ever touches a Muslim, but it becomes a source of forgiveness
for him – even the pinch of a thorn in his feet.” [
“No hardship befalls a servant of Allah, be it light or tough, but it
is always due to his sin – and the sins Allah forgives (without
afflicting with a calamity) are much more.” [Tirmidhi]
Also read This and this.
I hope i helped.
Allah knows best