Don't react to the button by changing the LED; change a state-variable in memory, instead. That saves the information of which button was most recently pressed. Then, each time through your loop, set your LED's according to the contents of that persistent variable. Remember that it will have to be a static variable (global or static local), as an automatic one will disappear and reappear after each exit and re-entry to loop(), and you cannot count on its contents when that happens.
How do I prevent overwriting the state variable with different input ?
You don't prevent it - you must over-write it. The purpose of the state variable is to remember which button was last pressed. I had in mind something like the following, where
lastButton saves the number of the most recent button:
pinmode(BUTN1PIN, INPUT_PULLUP); // you might not want the pullup
pinmode(BUTN2PIN, INPUT_PULLUP); // depending on your wiring.
static uint8_t lastButton = 0;
// Save the number of the last button pressed.
if (digitalRead(button1) == HIGH)
lastButton = 1;
else if (digitalRead(button2) == HIGH)
lastButton = 2;
// Set the LEDs according to most recent button
// Note that at program-start, when no button has
// been pressed yet, this code leaves both LED's off.
if (lastButton == 1)
else if(lastButton == 2)