As a software developer, I have found myself defending the code I wrote many times. But I think that this is a more generic question as it's not "should I have defended my code" it is
Should I apologize?
And my answer is
Did you offend them? Were you rude? Were you arrogant? Even if not, when the interviewer was arguing with you, did the tone of your voice give off a "what's this B/S you are telling me now" attitude? Then yes apologize. But not for having the courage of your own opinion. Apologize for your attitude. Apologizing or not is (IMHO) not a question for workplace specifically, it is for interpersonal skills in general. Try to apologize (again IMHO) only when you've hurt someone, not if you did what you thought was right and they simply didn't like it. Otherwise, you will not only find yourself apologizing way too often, but you will also give the impression that whenever someone tries to put the blame on you, even wrongfully, you will gladly accept it and apologize.
Lessons - Notes for the future
Now it seems a bit obvious to me that you are in the process of finding a new (or perhaps your first job). In my opinion, every interview should be a good lesson for the future, regardless of its outcome. You can do the same for this one.
Have in mind that you are not yet in their payslip, so you don't have any obligation to agree with anything they tell you specifically for the code, because they don't pay you yet to write their software. If you have cultural differences, now it's the time to unveil them without forgetting your manners. The trap here is that if you both insist, you will end up in a non-technical argument, and then it's a whole other situation. It's better to stop the argument in a polite "let's agree to disagree" manner, and move on. One way to exit that situation is in ray's comment who he actually suggests to throw the ball in their court, and let them elaborate what the problem with your solution was.
There are multiple benefits from that: first, this is an exit strategy. Your question was should I apologize, the answer is not to get yourself in that position at first. By asking them to elaborate rather than defending your solution minimizes the risk. Then you have them explain to you their disagreement. If it's for the sake of disagreement, you've exposed them. If there is a real reason behind that, then you can see how can you resolve these in the future, should you work with that person in the future.
Finally, always remember that the interviews work both ways: they interview you and you interview them as well. And there is no way to improve your skills other than practice (as sad as it might be). So ask yourself these: Do you want to work with them? Do you want to work with someone who argues for simple things and makes big issues out of nothing? Do you want to work for a company that promotes this culture? Even if this is a test as others have mentioned, it emits a message like
listen buddy, here we want people that say "yes sir" when we tell them that "the sky is green". I'm exaggerating of course, but if they want to prepare you and test you for these situations, then this means that this is the norm. Or that they have lost/fired employees for that reason and it's been such a major issue to cover it in the interview.
Disclaimer: How much you stick to the above, heavily depends on how badly you want the job. If it's your first one, then you might want to apologize even if it's somebody else's fault. But then just be careful not to become a "yes-man" by habit down the road.